If I have a tableView setup in an iPhone application with many rows, how can I update just one of those rows? I'm aware that they manually refresh as they come into view, but I'm looking to push out an update, for the开发者_开发技巧 sake of argument a timer counting down.
Thanks
Thanks for the answers, I found another method though!
By calling this:
NSIndexPath *a = [NSIndexPath indexPathForRow:0 inSection:0]; // I wanted to update this cell specifically
CustomTableViewCell *c = (CustomTableViewCell *)[theTableView cellForRowAtIndexPath:a];
I could then directly modify the label stored in my CustomTableViewCell, like so:
[c nowPlayingTrack].text = ...
Hope this helps someone else one day!
I'm not sure why but just calling reloadData on the tableView was causing run time errors because of the custom label. :(
You can just reload a single row by calling
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
For indexPaths you have to make an array that includes the indexPath of the row you want to reload. Row animation just describes whether and how the row should be reloaded, with or without animation:
typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle
}
If you don't know the indexPath, you can get it by calling
+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
I assume you're using some type of custom UITableViewCell....
What I normally do is create an array of my custom UITableViewCells, and populate my UITableView from those. This way, I can refresh the underlying object(s) (in this case your UITableViewCell's timer property) and the refresh the UITableView.
You might be able to access it directly (forgive my lack of actual iphone syntax.. it's been several months)
You might be able to access it directly, but I've had problems doing it that way.
[[[myUITableView cells] itemAtIndex:14] setTimerValue:WHATEVER];
精彩评论