zoukankan      html  css  js  c++  java
  • 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,单条刷新,完满解决~!



  • 相关阅读:
    CCS过渡和动画
    使用Vue.prototype在vue中注册和使用全局变量
    vuex的使用
    Vue组件懒加载
    JavaScript 箭头函数(Lambda表达式)
    JS命名规范
    css命名规范
    vertical-align和line-height的理解及实例
    </2017><2018>
    给当当同学的random data
  • 原文地址:https://www.cnblogs.com/riskyer/p/3271537.html
Copyright © 2011-2022 走看看