繁体   English   中英

如何在UICollectionView单元格的单元格中添加计数器?

[英]How do I add a counter to a cell in UICollectionView cells?

我有以下代码:

- (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方法中添加计数器(可能只是一个标签?)。 注册您的班级以获取所需的单元格标识符

[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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM