Update value in table cell on iPhone

妖精的绣舞 提交于 2019-12-05 01:54:47

问题


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


回答1:


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. :(




回答2:


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



回答3:


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];


来源:https://stackoverflow.com/questions/3577031/update-value-in-table-cell-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!