Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UITableView to reload properly?

I am creating an exercise based app in iOS. I feature that I would like to include is a favorites list. As it currently stands, I have an exercise library where you can tap a button and add it to a favorites list which is accessed through a navigation controller. I am able to properly pass titles to a store which are then placed in the favorites table view. I can load the specific exerciseViewController based on the title of the table view cell and save between run. However, I have a problem when I try to delete exercises. I have been struggling for hours.

This is the scenario:

I have added two exercises to my favorites: exercise A which is in the first row, and exercise B which is in the second. However, when the favorites list is accessed, I select exercise A to view its details, and decide to unfavorite it, and navigate back (go back) to the favorites list, it is not updated properly. While it correctly loads the number of rows (which is now 1), it still shows exercise A title (it should show exercise B title). But when I navigate all the way back to the home page, and select the favorites exercise, it shows correctly-now only showing the exercise B title. I have tried calling [[self tableview] reload] in viewWillAppear (this seems to be the most common answer), writing notifications, protocols, etc. and am still having this problem. The only time it seems to work properly is when the favorites list is accessed through the home page and viewDidLoad is called.

How can I get the table to show properly when the table appears and not having to load?

Favorites tableview

FavsVC.m  
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData]; 
    NSLog(@"APPEARING");
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
        NSString *name = [fav objectAtIndex:indexPath.row];
        cell.textLabel.text = name;
    }
    return cell;
}

//This the method is in my store, and gets called when I click on an exercise to add or delete.
- (id)addToFavorites:(NSString *)title
{
    favoriteTitles = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
    if ([favoriteTitles containsObject:title]) {
        NSLog(@"exercise removed");
        //[favoriteTitles removeObject:title];

        NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles];
        [newArray removeObject:title];
        [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"];  //newArray
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        NSLog(@"exercise added");
        //[favoriteTitles addObject:title];

        NSMutableArray *newArray = [NSMutableArray arrayWithArray:favoriteTitles];
        [newArray addObject:title];
        [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:@"Favorites"];  //newArray
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    return self;
}
like image 422
BradRS Avatar asked Jan 12 '14 08:01

BradRS


1 Answers

You don't create new cells each and every time, that's why you end up fetching "the wrong cells". You use reusablecells, which doesn't get created each time in cellForRowAtIndexPath, rather modified. The reason is that the UITableView only stores the cells that are actually on the screen for memory purposes. Use this instead.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
            // Only create a new cell if it's nil
        }
        // Now you have a reference to the cell. Set its variables.
        NSArray *fav = [[NSUserDefaults standardUserDefaults] objectForKey:@"Favorites"];
        NSString *name = [fav objectAtIndex:indexPath.row];
        cell.textLabel.text = name;
        return cell;
    }
like image 141
Martol1ni Avatar answered Oct 03 '22 17:10

Martol1ni