cell重用的几种方式
1.使用xib重用
?//ios6 之后推薦大家使用的重用方式
? ? //動態的使用self獲得當前類名,來作為唯一的標示
? ? NSString * identifier = NSStringFromClass([self class]);
? ? UINib * nib = [UINib nibWithNibName:identifier bundle:nil];
?? ? ? ? //注冊
? ? [tableView registerNib:nib forCellReuseIdentifier:identifier];
?? ?
? ? //先在緩存池中去,如果緩存池沒有可重用的cell,那么根據前面注冊的nib創建一個cell對象給你返回回來
? ? GPSubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
?? ?
? ? return cell;
?
2,純代碼
?
?
? ? //保證標示的唯一性,使用類名是一種非常好的選擇
?? ?
? ? NSString * identifier = @"GPSubjectCell";
?? ?
? ? //1.先去緩存池中找是否有可以重用的
? ? GPSubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
? ? //2.如果沒有可以重用的那么自己創建一個出來
? ? if(cell == nil)
? ? {
? ? ? ? cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
? ? }
?? ?
? ? return cell;
3.
//使用注冊的機制,實現純代碼cell類的重用
? ? NSString * className = NSStringFromClass([self class]);
? ? [tableView registerClass:[self class] forCellReuseIdentifier:className];
? ? return [tableView dequeueReusableCellWithIdentifier:className];
?
4.使用storyboard 時, (但必須給cell一個Identifier)
return? [collectionView dequeueReusableCellWithReuseIdentifier:@"bookCell" forIndexPath:indexPath];
轉載于:https://www.cnblogs.com/ouyangxiaoyao/p/5663393.html
總結
以上是生活随笔為你收集整理的cell重用的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建立集群间ssh信任关系
- 下一篇: C++ 类模板四(typename关键字