码迷,mamicode.com
首页 > 其他好文 > 详细

UI基础(四)之tableView

时间:2016-11-04 07:42:45      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:row   做了   number   ble   ring   ber   --   color   init   

---恢复内容开始---

1.Cell的重用机制:

如下图所示:我们在写tableview的数据源方法的时候,在第三个方法中通常会碰到定义重用cell的三步骤

 

#pragma mark -- 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.定义重用标识符
    static NSString *identifier = @"cellID";
    //2.根据重用标识符去缓存区中查找重用cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //3.判断是否有重用cell
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

 

技术分享

cell重用机制:如上图:最开始的时候,tableview是没有任何cell,此时缓存区也没有重用的cell,数据源方法会走(

 //2.根据重用标识符去缓存区中查找重用cell

)这个方法,显然是找不到的,那么继续走第三个方法,创建一个cell,仔细看这个方法,是带有重用标识符的,这个方法做了什么呢?

如下图:这个方法给创建好的cell一个重用标识符,然后再走(

cellForRowAtIndexPath

)方法,继续创建,直到创建完成,当我们上下拖动时,就会调用缓存区中的重用cell,从而实现cell的重用.

 

技术分享

 

 

 

 

 

 

 

---恢复内容结束---

UI基础(四)之tableView

标签:row   做了   number   ble   ring   ber   --   color   init   

原文地址:http://www.cnblogs.com/yiyuanchenfeng/p/6028915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!