UITableView 加载cell 的几种常用方式
2016-11-08 21:49
651 查看
开发的过程中,由于经常用到tableview,加载cell,也经常忘记所有,记录下经常用的几种方式
1 代码加载cell
这种经常用的方式,也是最早开始用的方式,加载比较麻烦,代码量比较多;
2 tableview registerClass 的方式加载
首先在viewdidload 方法注册cell
然后
这样创建cell的方式,cell 不会为空,所有也不需要判断cell==nil 的代码了,
3 通过xib 文件加载cell
这样加载和第一种代码加载的结果一样,cell 开始是空的,一屏幕划过去后,cell 就不会加载后,不会为空,开始重用,
4 用注册的方式加载xib 文件
这种和第第二种代码注册结果是一样的,
5 通过storyboard 创建的tableview cell 加载方式
sb 创建更简单化了cell 的加载方式
以上就是cell 加载的几种方式,又不对的地方还望指正,谢谢
1 代码加载cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *idfity=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row]; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idfity]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:idfity]; } return cell; }
这种经常用的方式,也是最早开始用的方式,加载比较麻烦,代码量比较多;
2 tableview registerClass 的方式加载
首先在viewdidload 方法注册cell
[tableView registerClass:[TableViewCell class] forCellReuseIdentifier:IDENTIFIER];
然后
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER]; //这里会发现,cell一直都不是nil,不再需要创建 if (cell == nil) { } return cell; }
这样创建cell的方式,cell 不会为空,所有也不需要判断cell==nil 的代码了,
3 通过xib 文件加载cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = nil; cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER]; if (cell == nil) { cell = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil].firstObject; } return cell; }
这样加载和第一种代码加载的结果一样,cell 开始是空的,一屏幕划过去后,cell 就不会加载后,不会为空,开始重用,
4 用注册的方式加载xib 文件
[tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:IDENTIFIER]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = nil; cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER]; if (cell == nil) { //cell一直都不是nil,不再需要创建,所有也不需要判断为空了 } return cell; }
这种和第第二种代码注册结果是一样的,
5 通过storyboard 创建的tableview cell 加载方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { XXXTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"XXXTableViewCell"]; [cell updateView:receiver]; return cell; }
sb 创建更简单化了cell 的加载方式
以上就是cell 加载的几种方式,又不对的地方还望指正,谢谢
相关文章推荐
- UITableView和UICollectionView的Cell高度的几种设置方式
- 总结了几种设置UITableView的cell动态高度的方法
- Flex加载图片的常用的几种方式
- ViewGroup 添加移除View 常用几种方式的区别
- Flex加载图片的常用的几种方式
- UITableView 滑动到最后一个cell自动加载下一页的数据
- 学习IOS开发UI篇--UITableView中Cell的创建方式总结
- iOS - UITableView加载网络图片 cell适应图片高度
- Uitableview中didSelectRowAtIndexPath常用的获取cell信息的方法
- ImageView加载图片的几种方式
- iphone开发之表格组件UITableView的使用(三)通过加载plist文件字典转模型方式展示分组数据
- Android的WebView加载网页的几种方式及网络异常处理
- 几种UITableview自定义Cell的办法
- UITableView获取cell的indexPath.row值(多种方式)
- UITableView延伸:点击cell关闭键盘,加载不同cell,监听里面的textfeild内容改变
- UITableView Cell一些常用的属性
- IOS--常用控件--UITableView--xib自定义cell
- ios tableView那些事 (十九) UITableView 加载动态cell
- 关于iOS UITableView 数据源数组加载完成之后,滑动时出现cell为空的问题解决笔记