数据源变更时,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;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值