简体   繁体   English

在iPhone上更新表格单元格中的值

[英]Update value in table cell on iPhone

If I have a tableView setup in an iPhone application with many rows, how can I update just one of those rows? 如果我在iPhone应用程序中有一个包含多行的tableView设置,如何仅更新其中一行? 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: 然后,我可以直接修改存储在CustomTableViewCell中的标签,如下所示:

[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. 我不确定为什么,但由于自定义标签,仅在tableView上调用reloadData会导致运行时错误。 :( :(

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. 对于indexPath,您必须创建一个数组,其中包含要重新加载的行的indexPath。 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 如果您不知道indexPath,可以通过调用

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section

I assume you're using some type of custom UITableViewCell.... 我假设您正在使用某种类型的自定义UITableViewCell。

What I normally do is create an array of my custom UITableViewCells, and populate my UITableView from those. 我通常要做的是创建一个自定义UITableViewCells数组,并从中填充UITableView。 This way, I can refresh the underlying object(s) (in this case your UITableViewCell's timer property) and the refresh the UITableView. 这样,我可以刷新基础对象(在本例中为UITableViewCell的timer属性),并刷新UITableView。

You might be able to access it directly (forgive my lack of actual iphone syntax.. it's been several months) 您也许可以直接访问它(原谅我缺乏实际的iphone语法。已经几个月了)

You might be able to access it directly, but I've had problems doing it that way. 您也许可以直接访问它,但是用这种方法我遇到了问题。

[[[myUITableView cells] itemAtIndex:14] setTimerValue:WHATEVER];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM