ios tableView reloadRowsAtIndexPaths无效

Atu*_*tia 10 reload uitableview ios

我有一个问题,我有一个UITableViewController,当视图出现时,我进行一些异步计算,这将导致更新表中的特定行.

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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

小智 7

包裹它怎么样?

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

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

在reloadRowsAtIndexPaths之后不调用cellForRowAtIndexPath

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

希望这可以帮助.

  • 谢谢,它成功了。我在 stackoverflow 上读到不需要开始和结束更新。我尝试重新加载路径中的行,但没有成功。但是用 ```self.tableView.beginUpdates()``` 和 ```self.tableView.endUpdates()``` 包装 ```reloadRowsAtIndexPaths``` **它起作用了**。有人可以解释为什么需要开始和结束吗? (2认同)

Par*_*iya 5

编辑:我认为您不indexindex检查部分可能是常量,因为您在遍历每个对象时增加:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

尝试这个 :

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

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

[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

  • 问题仍然存在-我必须将单元格滚动到视图之外才能更新。 (5认同)