iOS关于使用xib创建cell的3种初始化方式
转:https://blog.csdn.net/Bolted_snail/article/details/80244046
方案1:如果是在xib中已经将cell添加进去,则只需要给cell绑定标识(identfier),然后在cellForRowAtIndexPath:代理方法中通过dequeueReusableCellWithIdentifier:获取cell即可;
示例如下:
添加UITableView到Main.storyboard的wiew中,并设置代理数据源,然后添加cell到tableview中,并给cell绑定标识"JHCELL"
控制器中主要代码:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 30; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //直接通过标识去获取cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"]; cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row]; return cell; }
运行效果如下:
虽然这种方法很简单,但这种只是UITableViewCell,不是自定义,下面来看下用xib自定义的cell该如何实现。
方案2:如果是自定义cell,tableview要在viewDidLoad方法中注册要复用的cell,注册方法:- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier ;,如果是纯代码,注册方法为[self.tableView registerClass:[HJTableViewCell class] forCellReuseIdentifier:@"HJTableViewCell"];,然后可以在cellForRowAtIndexPath:数据源代理方法中调用dequeueReusableCellWithIdentifier方法获取cell。
具体示例如下:
xib自定义cell,可以不绑定标识:
#import "ViewController.h" #import "JHCell.h" @interface ViewController ()<UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设置行高 self.tableView.rowHeight = 100; //tableView注册要复用的cell [self.tableView registerNib:[UINib nibWithNibName:@"JHCell" bundle:nil] forCellReuseIdentifier:@"JHCELL"]; //不使用nib时注册cell //[self.tableView registerClass:[JHCell class] forCellReuseIdentifier:@"JHCELL"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 50; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //获取绑定标识的cell JHCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"]; cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row]; return cell; }
结果展示:
方案3:简单粗暴,先调用dequeueReusableCellWithIdentifier方法获取cell,如果取不到就直接创建,创建的方法:- (nullable NSArray *)loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options;。cell必须绑定标识才可以复用。
#import <UIKit/UIKit.h> @interface JHCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *indexL; +(instancetype)xibTableViewJHCell; @end #import "JHCell.h" @implementation JHCell +(instancetype)xibTableViewJHCell{ NSLog(@"%s",__func__); return (JHCell *)[[[NSBundle mainBundle] loadNibNamed:@"JHCell" owner:nil options:nil] lastObject]; } -(instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { //初始化子类 NSLog(@"%s",__func__); } return self; } - (void)awakeFromNib { [super awakeFromNib]; //设置子类 NSLog(@"%s",__func__); } @end //控制器 #import "ViewController.h" #import "JHCell.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设置行高 self.tableView.rowHeight = 100; //不使用nib时注册cell // [self.tableView registerClass:[JHCell class] forCellReuseIdentifier:@"JHCELL"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 50; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //获取绑定标识的cell JHCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"]; if (cell == nil) { cell = [JHCell xibTableViewJHCell]; } cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row]; return cell; } @end
效果同方案2。
xib创建视图注意事项参考文档
自定义UITableViewCell(registerNib: 与 registerClass: 的区别);
关于 initWithNibName 和 loadNibNamed 的区别和联系-iPhone成长之路;
iOS initWithFrame、initWithCoder、awakeFromNib的区别解析
————————————————
版权声明:本文为CSDN博主「面壁者LOGIC」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Bolted_snail/article/details/80244046
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!