多个UITableViewCell子类使用相同的xib?

tra*_*per 5 objective-c uitableview xib ios

如何xib在多个UITableViewCell子类中共享一个?

它们都具有相同的视图界面,​​因此我不希望多次重复相同的xib更改单元类.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:@"BaseCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"BaseCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseCell *baseCell = [tableView dequeueReusableCellWithIdentifier:@"BaseCell" forIndexPath:indexPath];

    ...
Run Code Online (Sandbox Code Playgroud)

我需要的是我的一个实例ChildCell- 这是我的一个子类BaseCell.有任何想法吗?

Dav*_*idA 2

使用组合而不是继承。BaseCell保持您的类与其 nib 中的单元格之间的 1-1 关系,为您创建的实现对象添加一个属性,并在将其出队后添加到单元格对象中。