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,单条刷新,完满解决~!



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值