【问题标题】:Null is returned from a method that is expected to return non-null value (UITableViewCell)从预期返回非空值的方法返回 Null (UITableViewCell)
【发布时间】:2017-07-11 10:49:54
【问题描述】:

我只是, 1) 出列单元格 2)检查零 3)根据情况设置单元格数据 并返回单元格。

这段代码有什么问题?我究竟做错了什么?只需检查nil

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GSDischargeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    [tableView registerClass:[GSDischargeListCell class] forCellReuseIdentifier:cellIdentifier];
}

if (_searchController.active && ![_searchController.searchBar.text  isEqual: @""] ) {
    GSDischargePatient *patient = searchResultsArray[indexPath.row];
    [cell setCellData:patient];
} else {
    GSDischargePatient *patient = datasourceArray[indexPath.row];
    [cell setCellData:patient];
}
   _totalHeight = [cell estimatedHeightOfCell];

   return cell;
}

【问题讨论】:

    标签: objective-c uitableview tableview


    【解决方案1】:

    首先,永远不要在cellForRowAtIndexPath 中注册单元格。如有必要,请在viewDidLoad 中注册您的手机(一次)。

    发生错误是因为如果cellnil.,您只是注册一个单元格而不是创建一个单元格

    更方便的方法是使用另一个dequeueReusableCellWithIdentifier 方法(dequeueReusableCellWithIdentifier:forIndexPath:),它总是返回一个非空的有效单元格。不需要检查nil。进一步将单元格转换为子类。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         GSDischargeListCell *cell = (GSDischargeListCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath: indexPath];
    
         if (_searchController.active && ... 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 2011-06-08
      • 2017-10-21
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多