Tableview 优化Cell的复用机制01

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tab=[[UITableView alloc]init];
    tab.frame=self.view.bounds;
    tab.dataSource=self;
    tab.rowHeight=70;//行高
    [self.view addSubview:tab];
}
#pragma mark-<UITableViewDataSource>数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}
//每当有cell进入屏幕中就会调用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cellaaa=[[UITableViewCell alloc]init];
////    cellaaa.textLabel.text=@"666";
////    %d->int
////    %zd->NSInteger
//    cellaaa.textLabel.text=[NSString stringWithFormat:@"6666---%zd",indexPath.row];
//    NSLog(@"%p-%zd",cellaaa,indexPath.row);
    
//    1.根据cell的标识去缓存池中查找可循环利用的Cell
    UITableViewCell *cellaaa=[tableView dequeueReusableCellWithIdentifier:@"a"];
//    2.如果cell为nil(缓存池找不到对应的cell)
    if(cellaaa==nil){
        cellaaa=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"a"];
//        cellaaa.textLabel.text=[NSString stringWithFormat:@"testdta---%zd",indexPath.row];
    }
//    3.覆盖数据
    cellaaa.textLabel.text=[NSString stringWithFormat:@"testdata---%zd",indexPath.row];
    
    return cellaaa;
}
@end

 

 

posted on 2016-11-12 17:16  高彰  阅读(345)  评论(0编辑  收藏  举报

导航