1

I have a simple tableview that's displaying a NSMutableArray, but when a item is added I need to do a [self.tableView reloadData];
How do I make the UITableView to update when the array is updated, like if I use the CoreData (NSFetchedResultControlerDelegate i think it is that does the updates).

Or do I need to accept the reloadData call on everyplace that's updating the array?

1 Answer 1

3

Try the following code

// adding new data to array.
[mArray addObject:@"abc"];

// Figure out where that item is in the array
int lastRow = [mArray count]-1;

// Create the corresponding index path
NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];

// Insert this new row into the table
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip]
                        withRowAnimation:UITableViewRowAnimationTop];

Note :

The important thing to keep in mind when using insertRowsAtIndexPaths is that your UITableViewDataSource needs to match what the insert is telling it to do. If you add a row to the tableview, make sure the backing data is already updated to match.

Further Reference

Apple Developer Docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.