最近因为工作需要开始学习iOS。在网上搜自动定义tableViewCell,出来的结果都是要新建xib文件和对应的class,然后在上面添加控件,再注册到tableViewController中。
这样做一来很繁琐,二来很容易在绑定outlet的时候抛异常(我就遇到了)。同事告诉我一个快捷的办法。

1、直接在StoryBoard里面将控件添加到Prototype Cell中。

Image of tableViewCell

2、新增一个Class(比如MyTableViewCell)继承UITableViewCell。

3、将Prototype Cell的Class设为MyTableViewCell。

4、按住control将控件拖到MyTableViewCell中,添加outlet。

1
2
@property (nonatomic, strong) IBOutlet UILable *label;
@property (nonatomic, strong) IBOutlet UIImageView *image;

5、直接调用

1
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTITY forIndexPath:indexPath];

搞定~