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

  • 方案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必须绑定标识才可以复用。
    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。

猜你喜欢

转载自blog.csdn.net/bolted_snail/article/details/80244046