在视图打开时默认显示先前选择的单元格,UICollectionView



我正在创建一个座位图,已经预订的座位很少,其他座位可以,所以,我需要用不同的颜色显示已经预订的座位和可用的座位。下面是我的代码,我用来显示选定和取消选择的单元格,但如何使显示已经预订的单元格已经选择。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"indexpath row:%d", indexPath.row);
int v;
NSString *cnt = [NSString stringWithFormat:@"%@",[arrSeats objectAtIndex:indexPath.section]];
v = [cnt intValue];
if(indexPath.item < v)
{
        if(cell.selected){
            cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yelow_seat.png"]];  // highlight selection
        }
        else
        {
            cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_seat.png"]]; // Default color
        }
        }else{
    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blank_seat.png"]];
}
return cell;
}

下面是我试图用来为每个部分添加值的代码。每次数组为一个节添加值时,它都会将其添加到字典中,然后清除数组,下一次数组为下一个节添加值并将其放入字典中,但当数组对象被删除时,字典将不再保存该值。

arrStatus = arrSeatsStatus;
        [seatsDict setObject:arrStatus forKey:[NSString stringWithFormat:@"%d",i]];
        i++;
        [arrSeatsStatus removeAllObjects];

我有一个数组,持有所有座位的状态为开或关,但如何实现它不得到。请指导以上内容。

查看此链接以显示单元格

http://developer.apple.com/library/ios/文档/WindowsViews/概念/CollectionViewPGforIOS/CreatingCellsandViews CreatingCellsandViews.html

或者做一个这个为cell创建一个标签或imageview并检查相应的元素是否与你的数组元素匹配,然后检查它们的状态,如果匹配,然后在该项目上放置一些图像或指定颜色如果不匹配,则将其留空。

如果它不工作,请让我,我将帮助你

最新更新