HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableView自定义单元格无法刷新大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
关于刷新自定义表格单元格有很多问题,但这个问题很奇怪.我有一个tableView,每个单元格,现在,有一个自定义UILabel和UI ImageView.

目前只有2个细胞.第一个显示日期.当表首次加载时,它会在第一个单元格UILabel中将当前日期显示为字符串.@H_262_7@

当我选择第一个单元格时,我会出现一个自定义类来处理所有日期选择.选择日期后,会弹出此视图,然后返回到表格视图.@H_262_7@

在 – (void)viewDidAppear上,重新加载tableviews数据并显示新选择的日期.@H_262_7@

但是,第一个单元格中的标签不会更新.@H_262_7@

令人困惑的是,如果我有多个单元格都显示相同的数据,这些将全部刷新并显示新的日期,如预期的那样.看起来索引:0处的单元格行不会刷新.@H_262_7@

进一步混淆的是当我查询单元格的UILabel的字符串值时,它返回正确的日期.@H_262_7@

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
{

    static NS@R_262_10495@ng *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:CellIdentifier];

        // 
        // clears the grouped style border from each table cell
        //

        UIView *clearBgView = [[UIView alloc]initWithFrame:CGRectZero];
        [cell setBACkgroundView:clearBgView];

        // label
        UILabel *dl = [[UILabel alloc]initWithFrame:CGRectMake(70.0f,10.0f,screenWidth-100,50)];
        [self setDetailsLabel:dl];
        dl = nil;

        [[self detailsLabel] setBACkgroundColor:[UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.1  ]];
        [[self detailsLabel] setTextColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:.3f]];

        //icon for each cell
        UIImageView *ci = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f,50.0f,50.0f)];
        [ci setBACkgroundColor:[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:.2]];
        [self setCellIcon:ci];
        ci = nil;

        //
        // set up views
        //

        [cell addSubview:[self cellIcon]];
        [cell addSubview:[self detailsLabel]];
    }

    // Configure the cell...
    [cell setSELEctionStyle:UITableViewCellSELEctionStyleNone];



    //populate each by row.
    NS@R_262_10495@ng *dateDisplay = [self formatDate:[self dateCaught]];
    NSLog (@"date is %@",dateDisplay);
   [[self detailsLabel] setText:[self formatDate:[self dateCaught]]];

    switch (indexPath.row) { //this needs to be an Integer,so return the row of the indexPath.
        case 0:
            NSLog (@"text for the cell is %@",[[self detailsLabel]text]);
            break;

        default:
            break;
    }


return cell;

}@H_262_7@

解决方法

问题与你传递detailsLabel的方式有关.你把它保存在自己的财产或ivar中

[self setDetailsLabel:dl];

但是只有在单元格无法重复使用时才设置它.当您重复使用单元格时,自我的detailsLabel将设置为先前运行的标签,从而导致各种问题.@H_262_7@

最干净的解决方案是创建自己的类,从UITableViewCell派生,将创建标签,图标,背景颜色等的初始化代码移动到指定的初始化程序中,并创建用于设置标签文本的属性.有了这个类,您就可以按如下方式简化代码:@H_262_7@

UIMyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:CellIdentifier];
}
[cell setSELEctionStyle:UITableViewCellSELEctionStyleNone];
[[cell detailsLabel] setText:[self formatDate:[self dateCaught]]];
// ^--- detailsLabel can be moved to UIMyCustomTableViewCell Now

大佬总结

以上是大佬教程为你收集整理的ios – UITableView自定义单元格无法刷新全部内容,希望文章能够帮你解决ios – UITableView自定义单元格无法刷新所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。