UITableView adds animation strings one at a time with noticeable delay

I am trying to add 10 rows to a tableView, but instead of reloadData I m using insertRowsAtIndesPaths in a for loop, but instead of adding one row at a time, it adds all 10 to the end. Here is my code ...

if([[[[[notification userInfo] objectForKey:@"cityevent"]objectForKey:@"events"]objectForKey:@"event"] isKindOfClass:[NSArray class]]) { [self.featuredEventTableView beginUpdates]; for(NSDictionary *tempDict in [[[[notification userInfo] objectForKey:@"cityevent"]objectForKey:@"events"]objectForKey:@"event"]) { ESEvent *event=[[ESEvent alloc]init]; event.name=[tempDict objectForKey:@"name"]; if([[tempDict objectForKey:@"image"]isKindOfClass:[NSNull class]] || [tempDict objectForKey:@"image"] ==nil) event.image=[UIImage imageNamed:@"category_default.png"]; else { event.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tempDict objectForKey:@"image"]]]]; } [events addObject:event]; NSIndexPath *ip=[NSIndexPath indexPathForRow:([events count]-1) inSection:0]; NSLog(@"row: %i section: %i",ip.row,ip.section); [self.featuredEventTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationRight]; [self.featuredEventTableView endUpdates]; } } 

Any suggestions?

+4
source share
4 answers

None of the answers worked. I used NSTimer as follows:

  tableTimer=[NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(performTableUpdates:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:i],@"count", nil] repeats:YES]; 

and

 -(void)performTableUpdates:(NSTimer*)timer { NSLog(@"%i",i); NSIndexPath *ip=[NSIndexPath indexPathForRow:i inSection:0]; [newArray addObject:[events objectAtIndex:i]]; [self.featuredEventTableView beginUpdates]; [self.featuredEventTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationLeft]; [self.featuredEventTableView endUpdates]; if(i<[events count]) { if([events count]-i==1) { [tableTimer invalidate]; i++; } else i++; } } 
0
source

Reschedule your update run, just before calling insertRowsAtIndexPaths: See if that helps. I am surprised that this works at all, because you call end update several times, but only start once.

In fact, it will probably be so fast that you won't even notice. NSTimer or GCD dispatch_after to deploy updates.

+1
source

The for loop does not matter in this case - the OS accumulates all the animation actions and binds them together. To do this, you will need to use the animation stop selector and call the external method to add the next cell until all cells have been added.

0
source

why don't you use reloadData ... I think this will help you with what you want to display in your user interface. If you use for the loop and add some data to your actual array, and in the end, if you call reload, this will give you this effect.

If you want to continue your existing code, try adding [NSThread sleepForTimeInterval:0.5]; after I guessed [self.featuredEventTableView endUpdates]; . I'm not on my poppy, so I can’t test it. This will pause the current thread for some time, so it will give an animation effect.

EDIT Try the code below

 if([[[[[notification userInfo] objectForKey:@"cityevent"]objectForKey:@"events"]objectForKey:@"event"] isKindOfClass:[NSArray class]]) { for(NSDictionary *tempDict in [[[[notification userInfo] objectForKey:@"cityevent"]objectForKey:@"events"]objectForKey:@"event"]) { ESEvent *event=[[ESEvent alloc]init]; event.name=[tempDict objectForKey:@"name"]; if([[tempDict objectForKey:@"image"]isKindOfClass:[NSNull class]] || [tempDict objectForKey:@"image"] ==nil) event.image=[UIImage imageNamed:@"category_default.png"]; else { event.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tempDict objectForKey:@"image"]]]]; } [events addObject:event]; NSIndexPath *ip=[NSIndexPath indexPathForRow:([events count]-1) inSection:0]; NSLog(@"row: %i section: %i",ip.row,ip.section); // If this don't work then add thread sleep code at here. [self.featuredEventTableView beginUpdates]; [self.featuredEventTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationRight]; [self.featuredEventTableView endUpdates]; } } 
0
source

All Articles