我有一个基本的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;
}
如何修复它以便在滚动后刷新正确的单元格? 干杯
答案 0 :(得分:2)
执行此操作时:
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"