I've UITableView
with dynamically changed data in cells by the time, i.e I have custom class for objects that store data for each cell 'AVMFilmsStore'. This class has method for updating data by himself (it gets progress from the sever every 10 seconds) like:
-(void)getProgress{
// some operations
...
if (progress<100){
[self getProgressWithDelay:10];
}
}
and getProgressWithDelay
method is:
-(void)getProgressWithDelay:(float)delay{
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(delay * NSEC_PER_SEC));
dispatch_after(time,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self getProgress];
});
}
After every call of this method I check if progress
value has changed and I need to tell to my main class with UITableView
that it must redraw every cell where progress has changed. I have custom UITableViewCell
class for this which contains method:
-(void)styleForReady:(BOOL)isReady andProgress:(float)progress{
if(!isReady){
self.movieDate.hidden = YES;
self.movieName.hidden = YES;
self.playButton.hidden = YES;
self.settingsButton.hidden = YES;
self.progressLabel.hidden = NO;
self.progressLine.hidden = NO;
self.backgroundImage.hidden = YES;
self.progressLine.transform = CGAffineTransformMakeScale(1.0, 4.0);
self.progressLine.progressTintColor = MINT_1_0;
self.progressLabel.textColor = [UIColor blackColor];
self.bgView.hidden = YES;
self.progressLabel.text = NSLocalizedString(@"movieCreating", nil);
[self.progressLine setProgress:progress animated:YES];
} else{
self.movieDate.hidden = NO;
self.movieName.hidden = NO;
self.playButton.hidden = NO;
self.settingsButton.hidden = NO;
self.progressLabel.hidden = YES;
self.progressLine.hidden = YES;
self.backgroundImage.hidden = NO;
self.bgView.hidden = NO;
}
And I call this method in cellForRowAtIndexPath
like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"readyCell";
AVMMovieCell *cell = [self.readyTable dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = (AVMMovieCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
AVMFilmsStore *oneItem = [readyArray objectAtIndex:indexPath.row];
if (oneItem.ready==1){
[cell styleForReady:YES andProgress:100];
} else { //если идет сборка
[cell styleForReady:NO andProgress:[oneItem getProgress]];
}
return cell;
}
To notify my class with cellForRowAtIndexPath
I register NSNotification in viewDidLoad
method of this class and then I send notifications in getProgress
of AVMFilmsStore
.
When my main class get notifications, it calls [tableView reloadData];
method and progressView
in each needed cell updates.
The problem is that updates of progressView
happens ONLY if I scroll my UITableView
though dataSource has changed.
It continues from my previous question Check progress where is necessary UITableView
I am quite confused with this problem. Any suggestions will be helpful. Sorry for so silly question but I have no idea how to solve it.
EDIT This is how I register for NSNotifications in main class:
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fsProgressChanged:)
name:@"filmStoreProgressChanged"
object:nil];
}
- (void)fsProgressChanged:(NSNotification *)notification
{
if ([[notification name] isEqualToString:@"filmStoreProgressChanged"]) {
[readyTable reloadData];
}
}
And how I send notifications in AVMFilmsStore
:
I have @property
readyProgress in this class and every time I get progress in this method I store progress into the readyProgress and then I do:
-(void)getProgress{
// some operations
...
if (progress<100){
if(self.readyProgress!=progress){
[[NSNotificationCenter defaultCenter] postNotificationName:@"filmStoreProgressChanged" object:nil];
}
[self getProgressWithDelay:10];
}
}