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.
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.
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!
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.
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.
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];
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!";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With