23

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.

akshayc
  • 444
  • 2
  • 9
pa12
  • 1,493
  • 4
  • 19
  • 40

3 Answers3

64

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];
CedricSoubrie
  • 6,657
  • 2
  • 39
  • 44
  • I am not familiar with index path. Can you please let me in detail. – pa12 Aug 12 '11 at 14:09
  • 1
    NSIndexPath as the name suggests holds an index. An index is made up of 2 components row and section. You can create one with [NSIndexPath indexPathForRow:(NSUInteger) inSection:(NSUInteger)]; – Abhinit Aug 12 '11 at 15:22
  • Each section can have multiple rows. In iPod MP3 player for example you have a section for each letter. Each of this section contains several artist. I've edited my original code to show you how to do. – CedricSoubrie Aug 12 '11 at 17:00
6

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!";
Hollance
  • 2,958
  • 16
  • 15
2

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

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
bosmacs
  • 7,341
  • 4
  • 31
  • 31