I have a tableView that needs to be updated after information has been inserted from another view. If I perform a
[self.tableView reloadData];
The very next time I insert more information in another view and try to reload the table, all the currently visible rows are duplicated.
In other words, when I start up the app I have:
tableView: Row 1 Row 2
Then I submit some information that will also show up in the table and suddenly I have:
tableView Row 1 Row 2 Row 3 <- info I just added Row 1 Row 2
My numberOfRowsInSection implementation looks like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [ItemsController sharedItemsController].count; }
My cellForRowAtIndexPath implementation looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ItemsController* controller = [ItemsController sharedItemsController]; NSMutableArray* recentItems = controller.listOfRecentItems; CustomCell *cell = nil; NSUInteger row = [indexPath row]; if( row < recentItems.count ) { Items* item = [recentItems objectAtIndex:row]; if( recentCellData == nil ) recentCellData = [[NSMutableDictionary alloc] initWithCapacity:[indexPath length]]; if( [recentCellData count] > 0 ) cell = [recentCellData objectForKey:[NSString stringWithFormat:@'%d', row]]; if (cell == nil) { UIViewController * view1 = [[UIViewController alloc] initWithNibName:@'CustomCell' bundle:nil]; cell = (CustomCell*)[view1 view]; [recentCellData setObject:cell forKey:[NSString stringWithFormat:@'%d',row]]; } // do some other stuff here } // Set up the cell return cell; }
What’s the best way to update the table and avoid duplicating the currently visible rows. Thank in advance for all the help!
The error isn’t in how you’re reloading the table, it’s in how you’re providing data to it. Set a breakpoint in the data source methods and the method that adds new rows to see where you’re going wrong.