【问题标题】:How to add different Custom Cells in one TableView with Storyboard?如何使用 Storyboard 在一个 TableView 中添加不同的自定义单元格?
【发布时间】:2013-07-25 15:57:15
【问题描述】:

我想通过使用情节提要在一个 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];
}

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

【问题讨论】:

    标签: xcode uitableview storyboard custom-cell


    【解决方案1】:

    在表格视图的属性检查器中,选择“动态原型”并在其下方选择原型单元格的数量。为每个单元格指定不同的标识符,并在将 cellForRowAtIndexPath 中的单元格出列时,使用基于 indexPath 的适当标识符。

    - (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...
    }
    

    【讨论】:

    • 谢谢!您能否发布一个示例,它如何使用基于索引路径的适当标识符?
    • 我已经按照你说的做了。我得到一个错误。 “Tableview 数据源必须从 tableview 返回一个单元格:cellforrowatindexpath” 也许我忘记了什么?我已将情节提要中的单元格链接到自定义单元格以及标签。 ...NSInteger 行 = [indexPath 行]; NSInteger 行 = [indexPath 行]; NSString *标识符; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (row == 0) {identifier =@"ThirdCell"; cell.clubNameLabel.text =@"%@",[clubname objectAtIndex:row]; }返回单元格;....
    • 你要么没有从dequeueReusableCellWithIdentifier 得到一个单元格,要么你没有从cellForRowAtIndexPath 得到它。根据您的评论,我猜是前者,因为您在为identifier 设置值之前调用了出队。在任何情况下,您都应该在cellForRowAtIndexPath 中设置一个断点,然后逐步查看哪里出错了。
    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多