【问题标题】:ios UICollectionView cells not refreshing after scrollios UICollectionView 单元格在滚动后不刷新
【发布时间】:2014-03-03 02:12:44
【问题描述】:

我有一个基本的 UICollectionView,如果我滚动它将“重绘”单元格顶部的标签,

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


    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];


    cell.backgroundColor=[UIColor greenColor];

    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
    [cell.contentView addSubview:title];

    //NSString *titleLbl = [NSString stringWithFormat:@"i = %d", indexPath.row];


    title.text = [self.arraya objectAtIndex:indexPath.row];

    return cell;
}

如何修复它以便在滚动后刷新正确的单元格? 干杯

【问题讨论】:

标签: uicollectionview reload


【解决方案1】:

当你这样做时:

UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
[cell.contentView addSubview:title];

您每次都在创建一个新标题并将其添加到您的 contentView。相反,请尝试向您的 UILabel 添加标签并更改现有 UILabel 的文本,如下所示:

UILabel *title = (UILabel *)[cell.contentView viewWithTag:SOME_NUMBER];
if (!title) {
    title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
    title.tag = SOME_NUMBER;
    [cell.contentView addSubview:title];
}
title.text = @"your new text"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多