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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17863417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:37:46  来源:igfitidea点击:

How to add different Custom Cells in one TableView with Storyboard?

xcodeuitableviewstoryboardcustom-cell

提问by Bolot NeznaJu

i want to add 2 or more diffrent Custom cells in one Tableview, by using storyboard. i know how to add diffrent cells without storyboard. I always do this by this way:

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

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];
}

And now my question: How i can do this with storyboard? To add one custom cell is possible. But i can not add two diffrent cell. Can you help me please?

现在我的问题是:我如何用故事板做到这一点?添加一个自定义单元格是可能的。但我不能添加两个不同的单元格。你能帮我吗?

回答by Timothy Moose

In the attributes inspector for the table view, select "Dynamic Prototypes" and below that select the number of prototype cells. Give each cell a different identifier and when dequeuing cells in cellForRowAtIndexPath, use the appropriate identifier based on indexPath.

在表格视图的属性检查器中,选择“Dynamic Prototypes”并在下方选择原型单元格的数量。为每个单元格提供不同的标识符,并在将 中的单元格出列时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...
}