Storyboard之Tableview

註冊cell經常使用方法:ide

設置全局變量static NSString *identifier = @"videoIdentifier";spa

1.viewDidLoad註冊cellit

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

2.cellforRow中關聯celltable

VideoCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];class

推薦使用封裝方法:變量

將cell註冊方法封裝在BaseTableViewCell基類裏(新建一個tableviewcell類)object

1.BaseTableViewCell.h select

+ (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];

相關文章
相關標籤/搜索