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



  • 相关阅读:
    ASP.NET MVC简介
    jQuery
    C#webform LinQ
    webform LinQ
    C# WebForm内置对象2+Repeater的Command
    C#WebForm内置对象
    JavaScricp
    Webform 内置对象 Session对象、Application全局对象,ViewState
    Webform 内置对象 Response对象、Request对象,QueryString
    WebForm复杂控件
  • 原文地址:https://www.cnblogs.com/riskyer/p/3271537.html
Copyright © 2011-2022 走看看