Storyboard之Tableview

注册cell常用方法:

设置全局变量static NSString *identifier = @"videoIdentifier";

1.viewDidLoad注册cell

[self.tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:identifier];

2.cellforRow中关联cell

VideoCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

推荐使用封装方法:

将cell注册方法封装在BaseTableViewCell基类里(新建一个tableviewcell类)

1.BaseTableViewCell.h 

+ (instancetype)cellWithTableView:(UITableView *)tableView;

2.BaseTableViewCell.m 实现

+ (instancetype)cellWithTableView:(UITableView *)tableView{

    NSString *className = NSStringFromClass([self class]);

    TPBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:className];

    if (!cell) {

        cell = [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil].firstObject;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    return cell;

}

3.引用时候:将自定义的cell基类更改为BaseTableViewCell如:

@interface TPChatSettingsTableViewCel : TPBaseTableViewCell

只需要在cellforrow中关联cell即可

TPChatSettingsTableViewCell *cell = [TPChatSettingsTableViewCell cellWithTableView:tableView];

 同理注册view

.h

+ (instancetype)createViewFromNib;

 + (instancetype)createViewFromNibName:(NSString *)nibName;

.m

+ (instancetype)createViewFromNibName:(NSString *)nibName

{

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];

    return [nib objectAtIndex:0];

}

 + (instancetype)createViewFromNib

{

    return [self createViewFromNibName:NSStringFromClass(self.class)];

}

引用时候

 self.alertCoinView = [TPAlertPetCoinView createViewFromNib];

原文地址:https://www.cnblogs.com/jiangxue-iOS/p/7602740.html