大佬教程收集整理的这篇文章主要介绍了ios – UITableView自定义单元格无法刷新,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目前只有2个细胞.第一个显示日期.当表首次加载时,它会在第一个单元格UILabel中将当前日期显示为字符串.
当我选择第一个单元格时,我会出现一个自定义类来处理所有日期选择.选择日期后,会弹出此视图,然后返回到表格视图.
在 – (void)viewDidAppear上,重新加载tableviews数据并显示新选择的日期.
令人困惑的是,如果我有多个单元格都显示相同的数据,这些将全部刷新并显示新的日期,如预期的那样.看起来索引:0处的单元格行不会刷新.
进一步混淆的是当我查询单元格的UILabel的字符串值时,它返回正确的日期.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *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. NSString *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;
}
[self setDetailsLabel:dl];
但是只有在单元格无法重复使用时才设置它.当您重复使用单元格时,自我的detailsLabel将设置为先前运行的标签,从而导致各种问题.
最干净的解决方案是创建自己的类,从UITableViewCell派生,将创建标签,图标,背景颜色等的初始化代码移动到指定的初始化程序中,并创建用于设置标签文本的属性.有了这个类,您就可以按如下方式简化代码:
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,请注明来意。