我有以下代码:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
如何在我的单元格中添加一个计数器以显示单元格的创建顺序?就像单元格 1 会说 1,2 是 2,依此类推。
最佳答案
创建 UICollectionViewCell
的子类。在单元格的 init 方法中添加计数器(也许只是一个标签?)。为您的类(class)注册您想要的单元格标识符
[self.collectionView registerClass:[MyCollectionViewCell class]
forCellWithReuseIdentifier:@"myCellIdentifier"];
并使带有标识符的单元格出列。然后设置标签值。
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.counterLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
关于ios - 如何向 UICollectionView 单元格中的单元格添加计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474186/