gpt4 book ai didi

ios - 如何在代码中设置 UITableView 的数据源

转载 作者:可可西里 更新时间:2023-11-01 05:36:08 27 4
gpt4 key购买 nike

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

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

}

我的第一个 View Controller 工作正常。它也可以很好地加载数据,所以我认为我在数据源对象中没有犯任何错误。但是每次我点击第二个选项卡时,程序都会立即崩溃。但是,如果我省略数据源分配语句,它不会崩溃。

这是崩溃:

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)

最佳答案

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

这就是导致您的应用崩溃的原因。 tableView:cellForRowAtIndexPath 方法的逻辑中的某些东西正在阻止返回 UITableViewCell。该日志消息说明了一切——UITableViewCell 必须从 tableView:cellForRowAtIndexPath: 返回。

关于ios - 如何在代码中设置 UITableView 的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com