gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:03:49 24 4
gpt4 key购买 nike

我有以下代码:

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com