Angela
Angela

Reputation: 11

[tableView reloadData]; doesn't reload until navigating back to root controller

A friend and I are working on a grocery list app and having some trouble with [tableView reloadData];. We're storing our grocery lists in plists and have an add items subview. When you leave that subview to return to the list, the list doesn't contain the new items even though we are updating the plist and calling reloadData in viewWillAppear. It does work though; if we navigate back to the master list of all lists and then back into the specific list we just added items to, the items then show up. Similarly, we want to delete items from the list by just selecting the row and having a checkmark appear next to the item. The list doesn't automatically update when a row is selected though, even though we're calling reloadData in didSelectRowAtIndexPath as well. Again, the change shows up if we navigate back to the master list and then go back into the individual list. I've checked and the delegate and dataSource in the nib files are correctly linked up to File's Owner.

Here's the code for the viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // THIS IS WHERE YOU PASS THE VARIABLE
    iGrocerAppDelegate *passTest = [[UIApplication sharedApplication] delegate];
    NSString *listName = [passTest passList];

    [passTest setPassList:listName];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *fullListName = [NSString stringWithFormat:@"%@.plist", listName];
    NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];

    self.list = [[NSArray alloc] initWithContentsOfFile:file];  

    [listTableView reloadData];
}

and didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:
                                    indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    int row = [indexPath row];
    NSString *deleteItem = [self.list objectAtIndex:row];
    [self.itemsToDelete addObject:deleteItem];



    // RECEIVE PASSED VARIABLE
    iGrocerAppDelegate *test = (iGrocerAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *listName = [test passList];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *fullListName = [NSString stringWithFormat:@"%@.plist", listName];     
    NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];

    //NSMutableArray *groceryLists = [[NSMutableArray alloc] initWithContentsOfFile:file]; 
    //NSMutableArray *groceryList1 = [[NSMutableArray alloc] initWithContentsOfFile:file]; 
    NSMutableArray *groceryList = [[NSMutableArray alloc] initWithContentsOfFile:file];

    NSMutableArray *removeFromList = self.itemsToDelete;

    int itemCount = [removeFromList count];
    NSLog(@"Count: %d", itemCount);

    for(int i=0; i < itemCount; i++) {
        NSString *item = [removeFromList objectAtIndex:i];
        NSLog(@"%@", item);
        [groceryList removeObject:item];
        NSLog(@"Middle ARRAY: %@", groceryList);
        //[groceryList addObjectsFromArray: addToList]; 
    }               

    NSLog(@"FINAL ARRAY: %@", groceryList);

    if(![groceryList writeToFile:file atomically:YES]) {
        NSLog(@"unsuccessful");
    } else {
        NSLog(@"successful");
    }

    NSLog(@"file name: %@",file);
    self.list = [[NSArray alloc] initWithContentsOfFile:file];  
    NSLog(@"updated grocery list: %@", self.list);

    [listTableView beginUpdates];
    //[listTableView reloadData];
    [listTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    [listTableView endUpdates];
    printf("list table view reloaded");
}

Any help would be much appreciated! Thanks in advance!

Upvotes: 0

Views: 2139

Answers (1)

Angela
Angela

Reputation: 11

Solved.

It just needed to be:

[self.tableView reloadData];

Upvotes: 1

Related Questions