首页 > 其他 > 详细

Tableview 优化Cell的复用机制01

时间:2016-11-12 19:08:03      阅读:129      评论:0      收藏:0      [点我收藏+]
#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

 技术分享

 

Tableview 优化Cell的复用机制01

原文:http://www.cnblogs.com/gaozhang12345/p/6057088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!