Search code examples
iosmultithreadinguitableviewnsnotificationsuiprogressview

UIProgressView in CustomCell never show when NSNotification was sent


I've got magazine app. There is UITableView which contains custom cells. In each cell is button, icon, title and hidden UIProgressView. When someone tap to button, pages (images) are starting to download. When some page is downloaded, I want to show hidden UIProgressView or update. And there's the problem. I'm using NSNotification and performSelectorOnMainThread for updating UIProgressView. But the UIProgressView doesn't show. I don't know where error is... Thx for reply!

There's some code... Creating 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];

Posting notificaiton:

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

Accepting notification:

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

Resend notification to main thread:

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

Update or show 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];
}

Solution

  • Hurray! Thx Tim for ideas! Your "simple project" helped me to solved it. Problem was, I start for loop. In this loop I'm posting notifications. But when for starts, it blocks main thread and UI hasn't been redrawn. So I sent calling of for loop to background and everything works fine. Thx again Tim