我在更改 View Controller 时遇到了一些崩溃问题——基本上我有一个 VC,它从用户那里获取数据进行搜索——然后它会打开一个带有搜索结果的新 VC,显示在 UITableView 中(不使用UITableViewController
,只是常规 UIViewController
中的一个 TableView)
崩溃日志:
2016-08-29 20:48:03.950 MyApp[2596:60877] *** Assertion failure in -[SearchResultsTable _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITableView.m:7971
2016-08-29 20:48:03.961 MyApp[2596:60877] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {375, 1128}>) failed to obtain a cell from its dataSource (; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}>)' *** First throw call stack:
我读到这个问题是由于没有返回您的cellForRowAtIndexPath
中的单元格引起的。功能,但是我很肯定我的实现将成功返回一个单元格。 (我在其他地方测试了函数的代码,cell
实际上确实有一个值,并且不是 nil。)
代码:@property searchResultCell * cell;
-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath *)indexPath { _cell = [self dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; if (_cell == nil) { _cell = [[searchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } [[_cell txtDate] setText:@"test"]; return _cell; }
设置了一些断点后,这部分代码似乎从未执行过,尽管UITableView
类、委托(delegate)和数据源设置正确。
搜索结果 VC 的 viewDidLoad([self SpecifiedSerial]
是在 segue 发生之前设置的。它是一个NSString*
):- (void)viewDidLoad { [super viewDidLoad]; _SearchResultsTableDelegate = [[searchResultsTable alloc] init:[self specifiedSerial]]; [[self SearchResultsTable] setDelegate:_SearchResultsTableDelegate]; [[self SearchResultsTable] setDataSource:_SearchResultsTableDelegate]; }
声明:@property (weak, nonatomic) IBOutlet UITableView *SearchResultsTable; @property searchResultsTable * SearchResultsTableDelegate;
如果有人能在这里指出我正确的方向,将不胜感激!
最佳答案
使用此代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
代替:
-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [self dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
编辑:
您的
numberOfRowsInSection
和 numberOfSectionsInTableView
似乎不正确。试试这个代码:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self searchedTrainSightings] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
if (_cell == nil)
{
_cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[[_cell textLabel] setText:@"test"];
return _cell;
}
关于ios - 尽管 cellForRowAtIndexPath 返回有效单元格,但 UITableView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39202233/