【发布时间】:2014-06-28 15:02:16
【问题描述】:
点击一个单元格后,我的单元格将背景变为黄色。当其背景为黄色时,它将恢复为默认颜色。
一切正常,但如果我开始滚动一些单元格,背景颜色会变为黄色,尽管它没有被选中。不幸的是,它具有随机行为。
我更改了使用alloc init 创建单元格,但现在它抛出了一个错误,上面写着:
cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:
我使用故事板来操作单元格中的元素并像使用它们一样
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"Number Cell"
forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = [NSString stringWithFormat:@"%i", indexPath.item + 1];
有什么建议吗?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"Number Cell"
forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = [NSString stringWithFormat:@"%i", (int)indexPath.item + 1];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
if ([cell.backgroundColor isEqual:[UIColor yellowColor]]) {
cell.backgroundColor = [UIColor clearColor];
} else {
cell.backgroundColor = [UIColor yellowColor];
}
}
我猜,
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
每当我点击一个单元格时都会调用它。因此,当颜色清晰时,我将颜色更改为黄色,反之亦然。
【问题讨论】:
标签: ios uikit uicollectionview uicollectionviewcell