KVO更新UITableViewCell

时间:2013-10-24 04:13:11

标签: iphone uitableview afnetworking key-value-observing

我有一个ViewControllerA,您可以使用AFNetworkingsetDownloadProgress下载文件,以便根据进度更新我的模型。我还有一个ViewControllerB,其UITableView包含所有传输的列表。

cellForRowAtIndexPath里面我观察我的模型

[transfer addObserver:cell
           forKeyPath:@"completed"
              options:NSKeyValueObservingOptionNew
              context:NULL];

这是有效的,我可以阅读像

这样的进展
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"completed"]) {
        float val = [[change objectForKey:@"new"] floatValue];
        NSLog(@"%f", val);
    }
}

但我不知道如何用我的KVO更新我的UITableViewCell?

1 个答案:

答案 0 :(得分:0)

我发现最简单的方法是继承UITableViewCell,并在那里添加你的观察结果。

@implementation MyCustomCell

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"completed"]) {
        float val = [[change objectForKey:@"new"] floatValue];

        // assuming that you're trying to update a label within your cell
        self.progressLabel.text = [NSString stringWithFormat:@"%f %%", val];
    }    
}
相关问题