ios - 如何在一个 tableView 中使用两个不同的自定义单元格?使用 Storyboard,iOS 7

标签 ios iphone objective-c uitableview custom-cell

我有两个自定义单元格。我想在我的 UITableView 中显示 2 个部分。第一个部分一行显示第一个自定义单元格,第二个部分显示从核心数据中提取的对象列表。

我应该如何实现“cellForRowAtIndexpath”方法?

这是我的一些代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
if (section == 0) {
    return 1;

} else if (section == 1) {
   //gastos is an array
   return [self.gastos count];  
}
return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath         *)indexPath
{

switch (indexPath.section) {
    case 0:
    {
        SaldoCelda *cell1 = [tableView dequeueReusableCellWithIdentifier:@"Cell1"      forIndexPath:indexPath];
        return cell1;
    }
    case 1:
    {
        static NSString *CellIdentifier = @"Cell";
        CeldaGasto *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier      forIndexPath:indexPath];

        NSManagedObject *gasto = [self.gastos objectAtIndex:indexPath.row];

        [cell.monto setText:[NSString stringWithFormat:@"%@ AR$", [gasto valueForKey:@"monto"]]];
        [cell.categoria setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"categoria"]]];
        [cell.fecha setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"fecha"]]];

        return cell;
    }
    default:
        break;
}
return 0;
}

这是我收到的错误信息:

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:6246 2014-03-05 01:02:57.181 Spendings[2897:70b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

谢谢你的帮助!

最佳答案

我已经做了测试,效果很好。这些是步骤:

  1. 在 Storyboard中创建 UITableViewController
  2. 将 UITableViewCell 拖放到已有单元格下方的 UITableViewController 上
  3. 将 CellIdentifier 分配给两个单元格(我使用了 Cell1 和 Cell2)
  4. 创建 UITableViewCell 的 2 个子类(我将它们命名为 Cell1 和 Cell2)
  5. 创建 UITableViewController 的子类并以某种方式命名
  6. 在 cellForRowAtIndexPath 方法中:

    static NSString *CellIdentifier = @"Cell1";
    
    static NSString *CellIdentifier1 = @"Cell2";
    
    switch (indexPath.section) {
    
        case 0:
        {
            Cell1 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            return cell;
        }
            break;
        case 1:
        {
            Cell2 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
            return cell;
        }
            break;
    
        default:
            break;
    

    return nil;
    

如你所见,实现与你的相同

我可以重现您的错误的唯一方法是在 switch block 中返回 nil 并且 dequeueReusableCellWithIdentifier 的文档说:

This method always returns a valid cell.

即使您弄乱了您的单元格标识符,您仍然不会收到您发布的错误。所以我的结论是:

重启,清理项目,重启模拟器或类似的东西导致你的场景根据文档是不可能的......

关于ios - 如何在一个 tableView 中使用两个不同的自定义单元格?使用 Storyboard,iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188537/

相关文章:

iphone - 从坐标中获取位置名称?

ios - swift 3 : only nil values in destination view controller of segue

ios - WKInterfaceTable、WKInterfaceButton 和操作方法

ios - 退出 .ipa 文件后,带有 Spotify 的 iPhone 应用程序崩溃

iphone - 将字符串格式说明符(%@、%d 等)保存在 NSString 中以供将来占位符使用?

iphone - 使用 OCunit 在 iOS 上测试异步代码

objective-c - 无法到达 navigationController,但应该可以。你能发现错误吗?

android - 为什么我使用 flutter_downloader 下载有时会失败?

iphone - 应用程序总是在 main.m 中终止

ios - ARC双向关系