【问题标题】:Prevent content in each UICollectionViewCell in UICollectionView from changing as I scroll?防止 UICollectionView 中每个 UICollectionViewCell 中的内容在我滚动时发生变化?
【发布时间】:2015-07-13 00:44:07
【问题描述】:

当我在 UICollectionView 中来回滚动时,我注意到屏幕上出现和消失的单元格中的内容发生了变化。

有什么方法可以防止这种情况发生吗? 我只想将所有内容适当地加载到每个单元格中,然后在滚动时防止它发生变化。

下面是一些与此相关的代码:

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

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];

    UILabel *label = (UILabel *)[cell viewWithTag:100];
    if (!label) {
        label = [[UILabel alloc] initWithFrame:cell.bounds];
        label.tag = 100;
        label.text = [self.array objectAtIndex:indexPath.row];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [cell.contentView addSubview:label];
    }

    return cell;
}

【问题讨论】:

    标签: ios objective-c scroll uicollectionview uicollectionviewcell


    【解决方案1】:

    问题是您没有为cellForRowAtIndexPath: 中的标签提供内容。请记住,这些单元格正在被重复使用。因此,即使标签已经存在,您也需要提供标签文本,因为单元格现在正在不同的行中使用,因此需要不同的文本!

    所以只需移动这一行:

        label.text = [self.array objectAtIndex:indexPath.row];
    

    ...在if 块的下方和外部,在您返回cell 之前。

    【讨论】:

    • 感谢您的解释:)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    相关资源
    最近更新 更多