* 方案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 ;然后可以在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: 的区别)
<https://blog.csdn.net/youngsblog/article/details/44536143>;
关于 initWithNibName 和 loadNibNamed 的区别和联系-iPhone成长之路
<http://blog.sina.com.cn/s/blog_7b9d64af01018f2u.html>;
iOS initWithFrame、initWithCoder、awakeFromNib的区别解析
<http://www.cnblogs.com/yajunLi/p/6344023.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信