首页 > 其他 > 详细

使用重用机制创建cell的两种方法

时间:2014-09-19 13:54:15      阅读:304      评论:0      收藏:0      [点我收藏+]
一.UICollectionView使用的cell重用机制 
1.首先服从<UICollectionViewDataSource>协议,如果自定义cell,导入自定义cell类的头文件

2.定义全局变量重用标识符
static NSString *cellIdentifier = @“重用”;

3.注册cell(collection,为UICollectionView对象)

[collection registerClass:[UICollectionCell class] forCellWithReuseIdentifier:cellIdentifier];   

  [collection release];


4.创建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

//使用自定义cell,用自定义cell类将UICollectionCell替换即可   

UICollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];         

    return cell;

}


二.UITableView使用的cell重用机制
1.首先服从<UITableViewDataSource>协议,如果自定义cell,导入自定义cell类的头文件

2.创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    (1).创建重用标识符  

   static NSString *identifier = @"重用;

    (2).根据重用标识符从队列中取出可重用的cell

//使用自定义cell,用自定义cell类将UICollectionCell替换即可      

   UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:identifier];

    (3).判断是否成功取到可重用的cell,如果没有创建一个cell   

     if (!cell) {         

            cell = [[[UITableViewCell alloc]       

     initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]   autorelease];

    }
       return cell;

}

使用重用机制创建cell的两种方法

原文:http://blog.csdn.net/hakusan/article/details/39396569

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