0

我一直在尝试创建一个显示多行的 UITableView(现在更具体的是 2 行),问题是,我需要从 XIB 文件中加载这 2 个自定义单元格。我已经创建了两个 UITableViewCell,但是当尝试让它工作时,应用程序会崩溃(SIGARBRT),我只能认为这是下面代码中的错误:

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

    static NSString *CellIdentifier1 = @"ACell";
    ACell *cell1 = (ACell *)[userSettingsTableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    static NSString *CellIdentifier2 = @"BCe;;";
    BCell *cell2 = (BCell *)[userSettingsTableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if([indexPath row] == 0) return cell1;
    if([indexPath row] == 1) return cell2;
    return nil;

    return cell1;
}

错误信息:

2012-05-05 18:21:49.256 StrangeThings[4388:f803] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-05-05 18:21:49.258 StrangeThings[4388:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x13cf022 0x1560cd6 0x1377a48 0x9b02cb 0xb3d28 0xb43ce 0x9fcbd 0xae6f1 0x57d42 0x13d0e42 0x1d87679 0x1d91579 0x1d164f7 0x1d183f6 0x1da5160 0x29f30 0x13a399e 0x133a640 0x13064c6 0x1305d84 0x1305c9b 0x12b87d8 0x12b888a 0x19626 0x1af2 0x1a65 0x1)
terminate called throwing an exception(lldb) 
4

1 回答 1

1

首先是更改您的单元格标识符,不要将其命名为您的类,在开始时使用带有小写字母的 like、cellA、cellB 并避免使用特殊字符。

其次是您没有为任何单元格分配内存,如果队列中没有单元格表视图将重用,那么它将不会返回任何单元格,请使用:

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

    static NSString *CellIdentifier1 = @"cellA";
    ACell *cell1 = (ACell *)[userSettingsTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1== nil) cell1 = [ACell alloc] init.........// your ACell class initlizer
    static NSString *CellIdentifier2 = @"cellB";
    BCell *cell2 = (BCell *)[userSettingsTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell2== nil) cell2 = [BCell alloc] init.........// BCell initializer
    if([indexPath row] == 0) return cell1;
    if([indexPath row] == 1) return cell2;
    return nil;

    return cell1;
}
于 2012-05-05T21:46:51.370 回答