在reloadRowsAtIndexPaths后不调用cellForRowAtIndexPath [英] cellForRowAtIndexPath isn't called after reloadRowsAtIndexPaths

查看:584
本文介绍了在reloadRowsAtIndexPaths后不调用cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Apple Doc 说,


重新载入列会导致表格检视要求其资料


我将UITableView与NSFetchedResultsController结合使用:

   - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{
if(self.tableView.isEditing)
{
[self.tableView setEditing:NO animated:YES];
}
[self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];

[self updateTabItemBadge];
[self.noDataView setHidden:![self isNoData]];

WXINFO(@controllerDidChangeContent);
}

在上述两个函数之间,我重新加载目标单元格:



>

  case NSFetchedResultsChangeUpdate:{
if(indexPath){
[tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationNone];
}



我在Line1563设置断点,检查 reloadRowsAtIndexPaths 已被调用,但之后, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath / p>

因此,我的单元格无法更新。



有人可以告诉我为什么?谢谢。

解决方案

这是我做的工作,但它做了工作。

  func tableView(_ tableView:UITableView,commit editingStyle:
UITableViewCellEditingStyle,forRowAt indexPath:IndexPath){
if editingStyle == UITableViewCellEditingStyle.delete {
if itemDetailDict.count - 1< = 0 {
self.mainTable.beginUpdates()
let cellOne = self.mainTable.cellForRow(at:indexPath)
cellOne? textLabel?.text =没有项目。
self.mainTable.reloadRows(at:[indexPath],with:.automatic)
self.mainTable.endUpdates()
} else {
self.mainTable.beginUpdates
let removeKey = Array(itemDetailDict.keys)[indexPath.row]
itemDetailDict.removeValue(forKey:removeKey)
mainTable.deleteRows(at:[indexPath],with:.automatic)
self.mainTable.endUpdates()
}
}
}


$ b b

基本上对删除行函数,我希望用户能够删除该行,但是当只剩下一行时,我想在tableview的单元格中显示一个默认消息。



为此,我更新了数据源,当字典中没有更多数据时,我更新了单元格以显示文本,重新加载单元格,然后调用endUpdating在我的tableview。



我希望这有助于。


As the Apple Doc said,

Reloading a row causes the table view to ask its data source for a new cell for that row.

I combine UITableView with NSFetchedResultsController:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    if (self.tableView.isEditing) 
    {
        [self.tableView setEditing:NO animated:YES];
    }
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];

    [self updateTabItemBadge];
    [self.noDataView setHidden:![self isNoData]];

    WXINFO(@"controllerDidChangeContent");
}

Between the above two functions, I reload the target cell:

    case NSFetchedResultsChangeUpdate: {
        if (indexPath) {
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }

I set a breakpoint at Line1563, to check that the reloadRowsAtIndexPaths has been called, but after that, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath wasn't called.

So, my cell could not be updated.

Somebody can tell me why? Thanks.

解决方案

This is a work around I did, but it did the job.

 func tableView(_ tableView: UITableView, commit editingStyle:
    UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete {
            if itemDetailDict.count - 1 <= 0 {
                self.mainTable.beginUpdates()
                let cellOne = self.mainTable.cellForRow(at: indexPath)
                cellOne?.textLabel?.text = "There are no items."
                self.mainTable.reloadRows(at: [indexPath], with: .automatic)
                self.mainTable.endUpdates()
            } else {
                self.mainTable.beginUpdates()
                let removeKey = Array(itemDetailDict.keys)[indexPath.row]
                itemDetailDict.removeValue(forKey: removeKey)
                mainTable.deleteRows(at: [indexPath], with: .automatic)
                self.mainTable.endUpdates()
            }
        }
    }

Essentially on the delete row function I wanted the user to be able to delete the row, but when only one row remained, I wanted to show a default message in the cell of the tableview.

To do this, I updated the datasource, and when there was no more data in the dictionary, I updated the cell to display the text, reloaded the cell, then called endUpdating on my tableview.

I hope this helps.

这篇关于在reloadRowsAtIndexPaths后不调用cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆