ios - 发送 NSNotification 时,CustomCell 中的 UIProgressView 从不显示

标签 ios multithreading uitableview nsnotifications uiprogressview

我有杂志应用程序。 UITableView 包含自定义单元格。每个单元格中都有按钮、图标、标题和隐藏的 UIProgressView。当有人点击按钮时,页面(图像)开始下载。下载某些页面时,我想显示隐藏的 UIProgressView 或更新。这就是问题所在。我正在使用 NSNotificationperformSelectorOnMainThread 来更新 UIProgressView。但是 UIProgressView 没有显示。我不知道错误在哪里...谢谢回复!

有一些代码... 创建 UIProgressView:

self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[self.progressView setFrame:CGRectMake(250, 200, 92, 28)];
[self.progressView setProgress:0.0];
[self.progressView setHidden:YES];
[self.cellView addSubview:self.progressView];

发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:kDownloadedIcon object:nil userInfo:dict];

接受通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iconDownloaded:) name:kDownloadedIcon object:nil];

向主线程重新发送通知:

- (void)iconDownloaded:(NSNotification *)notification {
[self performSelectorOnMainThread:@selector(updateProgressBar:) withObject:notification waitUntilDone:YES];}

更新或显示 UIProgressView:

- (void)updateProgressBar:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];

NSLog(@"%@ %@ %@", [dict objectForKey:@"name"], self.identifier, self.progressView);

if ([[dict objectForKey:@"name"] isEqualToString:self.identifier]) {
    if (self.spinner) [spinner stopAnimating];
    self.spinner = nil;
    [self.spinner removeFromSuperview];

    [self.progressView setHidden:NO];
    self.progress++;
    NSInteger count = [[dict objectForKey:@"pages"] intValue];
    [self.progressView setProgress:self.progress/count];
}

最佳答案

欢呼吧!谢谢蒂姆的想法!你的“简单项目”帮助我解决了它。问题是,我开始循环。在这个循环中,我发布通知。但是当 for 开始时,它会阻塞主线程并且 UI 没有被重绘。所以我将 for 循环调用发送到后台,一切正常。再次感谢提姆

关于ios - 发送 NSNotification 时,CustomCell 中的 UIProgressView 从不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12353984/

相关文章:

ios - 触摸开始 : inside UITableView

ios - 将旧数据模型转换为全新的数据模型(从头开始并删除其他所有内容)

ios - SQLite 查询在 iPhone 中不起作用

android - 来自另一个线程的回调导致异常

c++ - 是 x++;线程安全?

ios - 出列可重用 UITableViewCell 时属性字符串字体格式更改

ios - 重新加载 UITableView 时出现 NSInvalidArgumentException

iphone - 如何正确插入背景图片

ios - 从 iPhone 在开发 Mac 上运行命令

c++ - 多线程实时音频编程 - 阻塞或不阻塞