【问题标题】:KVO update UITableViewCellKVO 更新 UITableViewCell
【发布时间】:2013-10-24 04:13:10
【问题描述】:

我有一个 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?

【问题讨论】:

    标签: iphone uitableview afnetworking key-value-observing


    【解决方案1】:

    我发现最简单的方法是将 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];
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2015-09-12
      相关资源
      最近更新 更多