xcode - 如何使用 Storyboard 在一个 TableView 中添加不同的自定义单元格?

标签 xcode uitableview storyboard custom-cell

我想通过使用 Storyboard 在一个 Tableview 中添加 2 个或更多不同的自定义单元格。
我知道如何在没有 Storyboard 的情况下添加不同的单元格。我总是这样做:

static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//pictureCell = [[DetailPictureCell alloc]init];//(DetailPictureCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
pictureCell.header = true;
[pictureCell setHeader];
if (cell == nil) {
    if ([indexPath row] == 0) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HeaderAngebotViewCell" owner:self options:nil];
        NSLog(@"New Header Cell");
}
    if([indexPath row] ==1){
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"productCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
}

现在我的问题是:我怎么能用 Storyboard做到这一点?
可以添加一个自定义单元格。但我不能添加两个不同的单元格。你能帮我吗?

最佳答案

在表格 View 的属性检查器中,选择“动态原型(prototype)”并在其下方选择原型(prototype)单元格的数量。给每个单元格一个不同的标识符,并在 cellForRowAtIndexPath 中的单元格出列时,使用基于 indexPath 的适当标识符。

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier;
    if (indexPath.row == 0) {
        identifier = @"OneCellId";
    } else if (indexPath.row == 1) {
        identifier = @"OtherCellId";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //configure cell...
}

关于xcode - 如何使用 Storyboard 在一个 TableView 中添加不同的自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863417/

相关文章:

ios - 自动布局约束不适用于 iPhone 6 和 6 Plus

swift - 添加新的 View Controller B 并将其更改为初始 View Controller 后出现 "View Controller A unreachable because it has no entry points"错误

ios - .unredacted() SwiftUI - 占位符

ios - 创建一个新的Cocoapod : use example project for remote repo?

ios - UITableView 中最后选择的行

iOS UITableView 单元格选择

ios - 应用无法用 UUID 替换标签文本

ios - 创建新方案后找不到 Pod?

objective-c - 如何为 Mac (Cocoa) 应用程序创建端到端测试?

ios - 从编辑表单返回时更新 UITableView,刷新整个表数据源或被编辑的行?