Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios tableView reloadRowsAtIndexPaths not working

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

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

- (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 cellForRowAtIndexPath function is never called after and the cells do not get updated correctly. Any idea what the issue might be?

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?

like image 435
Atul Bhatia Avatar asked May 19 '14 06:05

Atul Bhatia


3 Answers

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

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

Hope this helps.

like image 198
Nathan Chang Avatar answered Oct 19 '22 10:10

Nathan Chang


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

- (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];
like image 5
Paresh Navadiya Avatar answered Oct 19 '22 09:10

Paresh Navadiya


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

DispatchQueue.main.async {
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
like image 3
Heysem Katibi Avatar answered Oct 19 '22 10:10

Heysem Katibi