Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload specific UITableView cell in iOS

Tags:

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.

like image 287
pa12 Avatar asked Aug 12 '11 13:08

pa12


People also ask

How do you reload a single cell?

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.

How do you refresh a tableView cell in Swift?

you can get Array of visible Cell by using TableView Function tableView. visibleRows() or You Can Get IndexPath of Visible Rows By tableView. indexPathsForVisibleRows() ! and then you can reload table by tableView. reloadData() Function!

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.


2 Answers

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]; 
like image 148
CedricSoubrie Avatar answered Sep 28 '22 06:09

CedricSoubrie


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!"; 
like image 40
Hollance Avatar answered Sep 28 '22 07:09

Hollance