UITableView的reloadData与reloadRowsAtIndexPaths:withRowAnimation:

只讨论cell 是nib的情况

reloadData:将当前页面上能看得到的cell 刷新一遍,从重用池中取的cell能与之前界面上的对应起来。

reloadRowsAtIndexPaths:withRowAnimation:  先到重用池中取出 cell,再移除将要替换的cell


举例:

点击按钮 进行刷新,第一次没有任何变化,第二次点击 会发现 4,5,6,7变成3,2,1,0

由此可见

第一次点击 :取出的cell 的content 是空的,随后将0,1,2,3放入重用池

第二次点击:取出的cell的content 依次为3,2,1,0

总结:reloadRowsAtIndexPaths:withRowAnimation:  先到重用池中按入池的顺序取出 cell,再依次移除目标cells

如下新建一个cell

.h

#import@interface QQTableViewCell : UITableViewCell

@property(nonatomic, copy)NSString *content;

@end

.m

#import "QQTableViewCell.h"

@interface QQTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *WQTitle;

@end

@implementation QQTableViewCell

- (void)awakeFromNib {

[super awakeFromNib];

// Initialization code

}

- (void)setContent:(NSString *)content {

_content = content;

self.WQTitle.text = content;

}

@end


ViewController.m

#import "ViewController.h"

#import "QQTableViewCell.h"

NSString *const cellID = @"QQTableViewCellID";

static int count = 0;

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *WQTableView;

@end

@implementation ViewController

- (void)viewDidLoad {   

 [super viewDidLoad];   

 self.WQTableView.dataSource = self;   

 self.WQTableView.delegate = self;    

[self.WQTableView registerNib:[UINib nibWithNibName:@"QQTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];    

// Do any additional setup after loading the view, typically from a nib.}

- (IBAction)touchToRefresh:(id)sender {    

//    [self.WQTableView reloadData];   

 [self.WQTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:count++ inSection:0],                                            

  [NSIndexPath indexPathForRow:count++ inSection:0],                                     

   [NSIndexPath indexPathForRow:count++ inSection:0],

[NSIndexPath indexPathForRow:count++ inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];}

#pragma mark -- 

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

{

QQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell.content) {

cell.content = [NSString stringWithFormat:@"%zd",indexPath.row];

}

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

//    return [[UIScreen mainScreen] bounds].size.height - 60;

return 44;

}

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

{

return 100;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容