ios - 尽管 cellForRowAtIndexPath 返回有效单元格,但 UITableView 崩溃

标签 ios objective-c uitableview uiviewcontroller

我在更改 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];

编辑:

您的 numberOfRowsInSectionnumberOfSectionsInTableView似乎不正确。

试试这个代码:
-(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/

相关文章:

ios - UITableView、UISwipeActionsConfiguration、UIContextualAction - pullView 不在 View 层次结构中。这是一个 UIKit 错误

ios - 滚动时 TableView 上的复选标记重复

ios - 在 Alamofire 中发布 Json 字符串

ios - UITableViewCell 在表格中使用插入单元格时不使用自动布局高度

iphone - 为什么这里使用 "error:&error"(objective-c)

ios - 使用查尔斯代理看不到 iOS 模拟器流量

ios - 如何在 Swift 中使用来自不同 View Controller 的多个核心数据实体

iOS:UIView 的自动布局拉伸(stretch)高度过多(Xcode 8、Swift 3)

ios - 尝试将非属性列表对象设置为 NSUserDefaults

iphone - Autorelease 上的自定义 UITableViewCell 导致应用程序崩溃