IOS UITableView单条刷新,数据不刷新解决方案

在使用 UITableView 进行某设置页面的设计时,由于设计页面有固定的section个数和row个数,而数据又需要根据用户的修改情况进行改变,所以我们往往不会为每个cell单独写一个类,而是直接对 contentView 添加子试图,如:

[cell.contentView addSubview:contentLab];

详细:

static NSString *Identifier = @"dentifier0";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
UILabel *titleLab,*contentLab;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:Identifier]; contentLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 20)];
contentLab.backgroundColor = [UIColor clearColor];
contentLab.textColor = [UIColor whiteColor];
[cell.contentView addSubview:contentLab];
}
contentLab.text = @"测试";
return cell;

当我们在用户操作后,执行单条刷新语句

NSIndexPath *refresh_row = [NSIndexPath indexPathForRow:0 inSection:2];
NSArray *array = @[refresh_row];
[_tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic];

数据并没有执行刷新

原因:在cell !=nil 的时候contentLab==nil

解决方案:

在else 的时候为contentLab 赋值,找到cell中已经创建的Label

如下:

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:Identifier]; contentLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 20)];
contentLab.backgroundColor = [UIColor clearColor];
contentLab.textColor = [UIColor whiteColor];
contentLab.tag = 10000;
[cell.contentView addSubview:contentLab];
}else {
contentLab = (UILabel*)[cell.contentView viewWithTag:10000];
}

ok,单条刷新,完满解决~!

上一篇:Go by Example


下一篇:关于Spark中RDD的设计的一些分析