当前位置: 首页 > news >正文

数据源变更时,table中更新对应cell的显示

要点是用cellForRowAtIndexPath取cell时,如果取得的是nil,则不用更新这个cell。因为在cell的重用机制下,这个cell在显示时会用新的数据去显示的,所以如果取得nil,则不用处理。



#import "ViewController.h"


@interface Person :NSObject


@property(strong)NSString* name;


@end


@implementation Person


@end


@interface ViewController ()

{

   NSArray* _dataSource;

}

@property (weak, nonatomic) IBOutletUITableView *_table;



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   

    NSMutableArray* persons = [NSMutableArrayarray];

   for(int i=0;i<20;i++){

       Person* person = [Personnew];

        person.name = [NSStringstringWithFormat:@"person%d",i];

        

        [personsaddObject:person];

    }

    

   _dataSource = [NSArrayarrayWithArray:persons];

    

    [self._tablereloadData];

}


- (IBAction)changeAction:(id)sender {

    //修改可见的cell

    {

       NSInteger index = 1;

       Person* person = (Person*)_dataSource[index];

        person.name = [NSStringstringWithFormat:@"change%ld",(long)index];

        

        

       NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:index inSection:0];

       UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];

        //没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。

       if(cell){

            cell.textLabel.text = person.name;

        }

    }

    

    //修改不可见的cell

    {

       NSInteger index = 9;

       Person* person = (Person*)_dataSource[index];

        person.name = [NSStringstringWithFormat:@"change%ld",(long)index];


       NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:index inSection:0];

       UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];

        //没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。

       if(cell){

            cell.textLabel.text = person.name;

        }

    }

    

    //修改不可见的cell

    {

       NSInteger index = 12;

       Person* person = (Person*)_dataSource[index];

        person.name = [NSStringstringWithFormat:@"change%ld",(long)index];

        

        

       NSIndexPath* myIndexPath =[NSIndexPathindexPathForRow:index inSection:0];

       UITableViewCell* cell = [self._tablecellForRowAtIndexPath:myIndexPath];

        //没有取到,则说明这个cell没有显示,在滚动显示,会用重用机制显示更改后的内容。

       if(cell){

            cell.textLabel.text = person.name;

        }

    }

}



#pragma mark - UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return_dataSource.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString* CellIdent =@"cell";

   UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdent];

   if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdent];

    }

    

   Person* person = (Person*)_dataSource[indexPath.row];

    cell.textLabel.text = person.name;

    

    cell.accessoryType  =UITableViewCellAccessoryNone;

   return  cell;

}



相关文章:

  • Visual C# 2005 - 如何于DataGridView控件中以跨数据行方式显示数据
  • iPhone在设置中修改应用相关项,应用会重启
  • 10月9日培训日记
  • 编译蘑菇街的teamtalk
  • HTML框架代码全集
  • 同一bundle id的应用,不同证书在设备上覆盖安装会失败
  • Xcode_6.4.dmg 官方下载地址
  • 从历史学习的 8 堂架构课
  • iOS错误报告中关于崩溃地址的分析
  • 原来Windows下面也有硬链接
  • 程序员将是人类最后一个被人工智能代替的工作
  • JNDI配置原理详解
  • 利用layer的FillRule属性生成一个空心的layer
  • 最近做的一个手机上的联网应用程序
  • Parse开源了自己的SDK
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【刷算法】从上往下打印二叉树
  • Angularjs之国际化
  • CAP理论的例子讲解
  • Create React App 使用
  • flask接收请求并推入栈
  • httpie使用详解
  • Vue.js-Day01
  • vue-loader 源码解析系列之 selector
  • webpack+react项目初体验——记录我的webpack环境配置
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 记一次和乔布斯合作最难忘的经历
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 我与Jetbrains的这些年
  • 验证码识别技术——15分钟带你突破各种复杂不定长验证码
  • 阿里云服务器购买完整流程
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​​​​​​​​​​​​​​Γ函数
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • # 飞书APP集成平台-数字化落地
  • #vue3 实现前端下载excel文件模板功能
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (30)数组元素和与数字和的绝对差
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (Java)【深基9.例1】选举学生会
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (学习日记)2024.01.09
  • (转)nsfocus-绿盟科技笔试题目
  • (转)大型网站架构演变和知识体系
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .net访问oracle数据库性能问题
  • .pop ----remove 删除
  • @RequestMapping处理请求异常
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [22]. 括号生成
  • [C#小技巧]如何捕捉上升沿和下降沿