UITableView自定义单元格不刷新
问题描述:
关于刷新自定义表格单元格有许多问题,但这个很奇怪。我有一个tableView,每个单元格现在都有一个自定义的UILabel
和UIImageView
。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, 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;
}
答
的问题与您通过detailsLabel
周围的方式做。你把它放在一个属性或伊娃在self
[self setDetailsLabel:dl];
但你设置它,只有当细胞是可以重复使用不。当您重复使用该单元格时,self
上的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
答
解决的办法是,不使用一个成员变量一样@property detailsLabel
,而是标签为所有自定义子视图。改变你的代码是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[...]
// label
UILabel *dl = [[UILabel alloc]initWithFrame:...];
dl.tag = 99;
[cell.contentView addSubview: dl];
[...]
}
[...]
UILabel *dl = [cell.contentView viewWithTag: 99];
[dl setText:[self formatDate:[self dateCaught]]];
[...]
return cell;
}
感谢您最近的更新,这正是我要做的。我试图通过像我这样快速定制单元格内容来尝试破解它,但正如你所看到的,遇到了问题。 – Tim