iOS关于使用xib创建cell的3种初始化方式

  • 方案1:如果是在xib中已经将cell添加进去,则只需要给cell绑定标识(identfier),然后在cellForRowAtIndexPath:代理方法中通过dequeueReusableCellWithIdentifier:获取cell即可;
    示例如下:
    添加UITableView到Main.storyboard的wiew中,并设置代理数据源,然后添加cell到tableview中,并给cell绑定标识"JHCELL"
    iOS关于使用xib创建cell的3种初始化方式_第1张图片
    控制器中主要代码:
-(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;
}

运行效果如下:
iOS关于使用xib创建cell的3种初始化方式_第2张图片

虽然这种方法很简单,但这种只是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,可以不绑定标识:
    iOS关于使用xib创建cell的3种初始化方式_第3张图片
#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;
}

结果展示:
iOS关于使用xib创建cell的3种初始化方式_第4张图片

  • 方案3:简单粗暴,先调用dequeueReusableCellWithIdentifier方法获取cell,如果取不到就直接创建,创建的方法:- (nullable NSArray *)loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options;。cell必须绑定标识才可以复用。
    iOS关于使用xib创建cell的3种初始化方式_第5张图片
#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的区别解析

你可能感兴趣的:(OC,iOS)