【问题标题】:How do I add a counter to a cell in UICollectionView cells?如何向 UICollectionView 单元格中的单元格添加计数器?
【发布时间】:2014-08-19 21:12:36
【问题描述】:
我有以下代码:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
如何在我的单元格中添加一个计数器以显示单元格的编号,按其创建顺序显示?就像单元格 1 会说 1,2 就是 2,依此类推。
【问题讨论】:
标签:
ios
ios7
uicollectionview
uicollectionviewcell
【解决方案1】:
创建UICollectionViewCell 的子类。在单元格的 init 方法中添加计数器(可能只是一个标签?)。为您的班级注册您希望的单元格标识符
[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;
}