ios UICollectionView 单元格在滚动后不刷新 [英] ios UICollectionView cells not refreshing after scroll

查看:66
本文介绍了ios UICollectionView 单元格在滚动后不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a basic UICollectionView that if I scroll will "redraw" the label on top of the cells,

- (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;
}

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

How to fix it so it refreshes the proper cell after scrolling? Cheers

推荐答案

当你这样做时:

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

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

You're creating a new title every time and adding it to your contentView. Instead, try adding a tag to your UILabel and changing your existing UILabel's text, like so:

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"

这篇关于ios UICollectionView 单元格在滚动后不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆