4

我知道这个问题是 stackoverflow 上的其他地方,但这些解决方案都没有对我有用。我有两个带有表视图的选项卡,我想使用相同的数据源。第一个选项卡的视图控制器是UITableViewController. 第二个是简单UIViewController的,它的 tableView 在 IB 中设置。我最初设置视图控制器初始化程序以将数据源作为参数,但由于它崩溃太多,我试图通过简单地在我的视图控制器中分配它来简化事情。我的表视图和数据源设置如下:

@property (weak, nonatomic) IBOutlet UITableView *tableView;  //Connected in IB
@property (strong, nonatomic) TableViewDataSource *data;

每当我发生以下情况时,我的程序总是崩溃:[self.tableView setDataSource: data];我尝试将该行放入viewDidLoad方法中,但我的程序仍然崩溃。这是我的viewDidLoad方法:

- (void)viewDidLoad
{

    [super viewDidLoad];
    data = [TableViewDataSource new];  //My data source object
    NSLog(@"%@", data);   //This isn't null, it says TableViewDataSource and then some address
    [self.tableView setDataSource:data];

}

我的第一个视图控制器工作正常。它也可以很好地加载数据,所以我不相信我在数据源对象中犯了任何错误。但是每次我单击第二个选项卡时,程序都会立即崩溃。但是,如果我省略数据源分配语句,它不会崩溃。

这是崩溃:

2013-08-14 12:56:57.313 iPlanner[961:c07] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:5471
2013-08-14 12:56:57.314 iPlanner[961:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x1ca2012 0x10dfe7e 0x1ca1e78 0xb75665 0xd9c1b 0x6e40c 0xd9a7b 0xde919 0xde9cf 0xc71bb 0xd7b4b 0x742dd 0x10f36b0 0x229efc0 0x229333c 0x2293150 0x22110bc 0x2212227 0x22b4b50 0x39edf 0x1c6aafe 0x1c6aa3d 0x1c487c2 0x1c47f44 0x1c47e1b 0x1bfc7e3 0x1bfc668 0x23ffc 0x298d 0x28b5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
4

1 回答 1

5
'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

这就是导致您的应用程序崩溃的原因。您的方法逻辑中的某些tableView:cellForRowAtIndexPath内容是防止 aUITableViewCell被返回。该日志消息说明了一切 - AUITableViewCell必须从tableView:cellForRowAtIndexPath:.

于 2013-08-14T19:22:42.353 回答