[英]UITableView crash despite cellForRowAtIndexPath returning valid cell
当我更改视图控制器时,我遇到了一些崩溃的问题 - 基本上我有一个VC从用户那里获取数据进行搜索 - 然后它会调出一个带有搜索结果的新VC,显示在UITableView中(不使用UITableViewController
,只是常规UIViewController
的TableView)
崩溃日志:
2016-08-29 20:48:03.950 MyApp [2596:60877] ***断言失败 - [SearchResultsTable _configureCellForDisplay:forIndexPath:],/ BuildRoot / Library / Cache / com.apple.xbs / Source / UIKit_Sim / UIKit- 3512.60.7 / UITableView.m:7971
2016-08-29 20:48:03.961 MyApp [2596:60877]由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView(; layer =; contentOffset:{0,0}; contentSize:{375,1128} >)无法从其dataSource获取单元格(; layer =; contentOffset:{0,0}; contentSize:{0,0}>)'***首先抛出调用堆栈:
我已经读过这个问题是由于你的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
类,委托和dataSource,但这段代码似乎永远不会被执行。
搜索结果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;
}
可能原因是你从cellForRowAtIndexPath
方法返回nil并且无法使可重用单元出列。
使用代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
searchResultCell *cell;
cell = (searchResultCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil){
cell = [[searchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[[_cell txtDate] setText:@"test"];
return cell;
}
只需配置您的单元格选择您的自定义单元cell
使用故事板查看图像 。
希望能帮助到你。
我严重怀疑你正在使用的委托方法。 我见过这个版本的cellForRowAtIndexPath
方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
尝试这个
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
_cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (_cell == nil)
{
_cell = [[searchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[[_cell txtDate] setText:@"test"];
return _cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.