gpt4 book ai didi

objective-c - 无法在 uicollectionview、UICollectionView 的特定 indexPath.row 处为单元格设置标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:52 24 4
gpt4 key购买 nike

在处理 UICollectionView 时,我正在从 nib 加载一个单元格,如下所示

- (void)viewDidLoad
{
[super viewDidLoad];
/* Uncomment this block to use nib-based cells */
UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


// Configure layout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView.contentInset = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);

[flowLayout setItemSize:CGSizeMake(300, 200)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flowLayout];
}

我的 nib 文件如下所示

enter image description here

最上面的标签用来显示单元格的个数,中间的标签用来显示某段文字

在方法 cellForItemAtIndexPath 中,我只想将文本设置到特定行的单元格(在这个例子中,我在第 1 行做):

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// Setup cell identifier
static NSString *cellIdentifier = @"cvCell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
UILabel *cellLablel = (UILabel *)[cell viewWithTag:200];
cellLablel.text = self.dataArray[0][indexPath.row];
if ( indexPath.row == 1)
[titleLabel setText:@"this is row 1"];

return cell;

}

运行应用程序时,出现问题。不仅是 row1 单元格的 titleLabel设置为 This is row 1,同时还设置了 row5、row9 和 row10 的 titleLabel: enter image description here

enter image description here

如果有人知道我在中间做错了什么。请帮忙。

我的收藏集包含 1 个部分和该部分的 15 行。

最佳答案

细胞被重复使用。一旦您向下滚动并且 Cell 1 变得不可见, Collection View 就会使其再次可用。只需为所有单元格设置文本,例如

titleLabel.text = [NSString stringWithFormat:@"this is row %d", indexPath.row];

关于objective-c - 无法在 uicollectionview、UICollectionView 的特定 indexPath.row 处为单元格设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520297/

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