76

how can I reload and animate just one cell/row ? Right now i download some files. Everytime a file finished downloading, i call it's finished delegate and call [tableview reload]. But then the whole table reloads. And how can i animate the table, so that it doesn't blink in. For example a fade effect or so.

greets Max

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
madmax
  • 1,803
  • 3
  • 25
  • 50

6 Answers6

188

Use the following UITableView instance method:

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

You have to specify an NSArray of NSIndexPaths that you want to reload. If you just want to reload. If you only want to reload one cell, then you can supply an NSArray that only holds one NSIndexPath. For example:

NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[myUITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

You can see the UITableViewRowAnimation enum for all the possible ways of animating the row refresh. If you don't want any animation then you can use the value UITableViewRowAnimationNone, as in the example.

Reloading specific rows has a greater advantage than simply getting the animation effect that you'd like. You also get a huge performance boost because only the cells that you really need to be reloaded are have their data refreshed, repositioned and redrawn. Depending on the complexity of your cells, there can be quite an overhead each time you refresh a cell, so narrowing down the amount of refreshes you make is a necessary optimization that you should use wherever possible.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • 17
    With the new syntax you can also do the following:[UITableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; – bbrame Jan 31 '13 at 18:07
  • @JamesBedford to reload single cell.Do I need to write some in - (void)reloadRowsAtIndexPaths method? – Prince Kumar Sharma Jun 17 '13 at 06:08
  • The reloadRowsAtIndexPaths:withRowAnimation: method is a method of UITableView that you can use to reload a single cell (just pass a single cell index path as an argument!) – James Bedford Jun 17 '13 at 16:58
  • How can I do this from the cell itself? I have a cell view subclass with a button and when I click on the button I want to update the cell from within, instead of having to deal with this in the table view controller. Can I do it? – Nuno Gonçalves Apr 09 '15 at 21:59
  • Have you tried having the cell have a reference to the table view controller and then just call `reloadRowsAtIndexPaths:withRowAnimation:` on its `tableView`? – James Bedford Apr 12 '15 at 04:23
  • Do i need to reload [myTableView reloadData] after [myUITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; and cellForRowAtIndexpath called once or array count time? – Avijit Nagare Aug 01 '15 at 03:18
15

Swift 4 & 5

For TableView

let index = IndexPath(row: 0, section: 0)
tableView.reloadRows(at: [index], with: .automatic)

For CollectionView

let index = IndexPath(item: 2, section: 0)
collectionView.reloadItems(at: [index])
Akbar Khan
  • 2,215
  • 19
  • 27
6

If you only need to update the text in the tableview cell..

UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

cell.textLabel.text = [NSString stringWithFormat:@"%i", (int)_targetMaxDistanceSlider.value];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
slammer
  • 278
  • 3
  • 9
2

To reload particular cells in a tableView at given indexPaths, you need to create an array of indexPaths and then call function reloadRowsAtIndexPaths on the tableView.

Here is the example:

Swift 5:

let indexPathsToReload = [indexPath1, indexPath2, indexPath3]
tableView.reloadRows(at: indexPathsToReload, with: .none)

Objective C:

NSArray *indexPaths = @[indexPath1, indexPath2, indexPath3];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];

or

NSArray *indexPaths = [NSArray arraywithobject:@"indexPath1", @"indexPath2", @"indexPath3",nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
Paul123
  • 18
  • 7
BLC
  • 2,240
  • 25
  • 27
2

Swift

You can use

let selectedIndexPath = IndexPath(item:0 , section: 0)
self.tableView.reloadRows(at: [selectedIndexPath], with: .none)

in order to reload a specific cell.

1

Apple Document done it with new syntax

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57