ios tableView reloadRowsAtIndexPaths 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23730773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:09:27  来源:igfitidea点击:

ios tableView reloadRowsAtIndexPaths not working

iosuitableviewreload

提问by Atul Bhatia

I have an issue where I have a UITableViewControllerand when the view appears I do some calculations asynchronously which should result in the updating of specific rows in the table.

我有一个问题,UITableViewController当视图出现时,我会异步执行一些计算,这应该会导致更新表中的特定行。

Inside the viewWillAppearfunction I calculate the necessary rows that need to be updated as follows:

viewWillAppear函数内部,我计算需要更新的必要行,如下所示:

- (void)reloadPaths:(NSMutableDictionary *)bookingsDict
{
    NSMutableArray* indexArray = [NSMutableArray array];
    int i = 0;
    for (NSString *dateKey in self.eventsToShow) {
        NSMutableDictionary *venues = [bookingsDict objectForKey:dateKey];
        if (venues) {
            int j = 0;
            NSArray *events = [self.eventsToShow objectForKey:dateKey];
            for (DCTEvent *event in events) {
                NSString *venueIdString = [NSString stringWithFormat:@"%@", event.eventVenueId];
                if ([venues objectForKey:venueIdString]) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
                    [indexArray addObject:indexPath];
                }
                j++;
            }
        }
        i++;
    }
    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}

However I notice that the cellForRowAtIndexPathfunction is never called after and the cells do not get updated correctly. Any idea what the issue might be?

但是我注意到该cellForRowAtIndexPath函数永远不会被调用,并且单元格没有正确更新。知道可能是什么问题吗?

EDIT: I just noticed that if I scroll out of the view of the cell that is supposed to get updated then it gets updated when I scroll back into view. Is there no way to have it update while in view?

编辑:我刚刚注意到,如果我滚动出应该更新的单元格的视图,那么当我滚动回视图时它会更新。有没有办法让它在视图中更新?

回答by Hsin Chang

How about wrap it?

包起来怎么样?

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

Here are some similar problems I have found.

以下是我发现的一些类似问题。

cellForRowAtIndexPath isn't called after reloadRowsAtIndexPaths

在 reloadRowsAtIndexPaths 后不调用 cellForRowAtIndexPath

UITableView's reloadRowsAtIndexPaths: (NSArray *) indexPaths failing to cause a reload unless you call it twice?

UITableView 的 reloadRowsAtIndexPaths: (NSArray *) indexPaths 无法导致重新加载,除非你调用它两次?

Hope this helps.

希望这可以帮助。

回答by Paresh Navadiya

EDIT: I think you are not getting indexPath check section might be constant as you are increasing while each object gets traversed:

编辑:我认为您没有得到 indexPath 检查部分可能是恒定的,因为您在遍历每个对象时正在增加:

- (void)reloadPaths:(NSMutableDictionary *)bookingsDict
{
    NSMutableArray* indexArray = [NSMutableArray array];
    int i = 0;
    for (NSString *dateKey in self.eventsToShow) {
        NSMutableDictionary *venues = [bookingsDict objectForKey:dateKey];
        if (venues) {
            int j = 0;
            NSArray *events = [self.eventsToShow objectForKey:dateKey];
            for (DCTEvent *event in events) {
                NSString *venueIdString = [NSString stringWithFormat:@"%@", event.eventVenueId];
                if ([venues objectForKey:venueIdString]) {
                    //changed here as section might be constant as i think it might be
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:0];
                    [indexArray addObject:indexPath];
                }
                j++;
            }
        }
        i++;
    }

    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

Try this :

尝试这个 :

//your code here
[self.tableView beginUpdates];

[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];

[self.tableView endUpdates];

回答by Heysem Katibi

For me pushing my reload code to the main queue solved the problem

对我来说,将重新加载代码推送到主队列解决了问题

DispatchQueue.main.async {
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
}