pa12
pa12

Reputation: 1503

Reload specific UITableView cell in iOS

I have a UITableView. I want to update the table data based on selection made. Right now I use [mytable reloadData]; I want to know if there is any way where I can just update the particular cell which is selected. Can I modify by using NSIndexPath or others? Any suggestions?

Thanks.

Upvotes: 23

Views: 39939

Answers (3)

CedricSoubrie
CedricSoubrie

Reputation: 6697

For iOS 3.0 and above, you just have to call :

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

To reload row 3 of section 2 and row 4 of section 3 for example, you'll have to do this :

// Build the two index paths
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2];
NSIndexPath* indexPath2 = [NSIndexPath indexPathForRow:4 inSection:3];
// Add them in an index path array
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, indexPath2, nil];
// Launch reload for the two index path
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];

Upvotes: 64

Hollance
Hollance

Reputation: 2976

You can also get a reference to the UITableViewCell object and change its labels, etc.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = @"Hey, I've changed!";

Upvotes: 6

bosmacs
bosmacs

Reputation: 7483

I think you're looking for this method of UITableView:

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Upvotes: 2

Related Questions