ios - 如何向 UICollectionView 单元格中的单元格添加计数器?

标签 ios ios7 uicollectionview uicollectionviewcell

我有以下代码:

- (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/

相关文章:

objective-c - touchXML:stringValue 未保存为字符串?

ios - 当应用程序进入后台时如何继续套接字通信(iOS7)?

iphone - 项目构建没有错误但未运行

iphone - IOS Urban Airship 。如何处理通知

ios - CLLocationmanager 委托(delegate)方法在 xcode6 中不起作用

ios - 如何防止两种类型的 SpriteKit 节点相互碰撞?

ios - 使用拉动刷新继续随机索引?

ios - 如何快速调整单元格大小以适应尺寸类别

ios - UICollectionView 在单元格之间添加间隙

ios - 无法在 swift 3 中获取 jsondata?