Tableview的4种注册方式

其实tableveiw只有两种注册方式,注册和非注册。但是因为有了xib所以两种方式里纯代码和xib又形成了两种写法,今天有时间总结到一起,方便大家看。

注册方式:

复制代码
注册cell

//向 tableView 注册 cell 的样式 如果是 纯代码的自定Cell 使用该方法
[self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"listcell"];

//向 tableView 注册 cell 的样式 这里需要tableView 根据 xib 帮我创建cell 对象
UINib *listnib = [UINib nibWithNibName:@"ListCell" bundle:nil];
[self.tableView registerNib:listnib forCellReuseIdentifier:@"listcell"];
复用cell

ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];
非注册方式:

复用cell

ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
if (!listCell) {
//xib方式
listCell = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:nil options:nil].firstObject;
//纯代码方式
ListCell=[[ListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListCell"];
}
复制代码

 

————————————————
版权声明:本文为CSDN博主「MIKE-zi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lee727n/article/details/91867064

posted @   brave-sailor  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2017-02-23 Swift 本地推送通知UILocalNotification
2017-02-23 Android滑动到顶部悬停
2017-02-23 在 Swift 项目中实现侧滑菜单-利用 SWRevealViewController
2016-02-23 Android状态栏颜色修改
点击右上角即可分享
微信分享提示