Akash Malhotra
Akash Malhotra

Reputation: 1106

UITableView adding rows with animation one at a time with notable 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 at the end. Heres 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?

Upvotes: 4

Views: 2399

Answers (4)

Akash Malhotra
Akash Malhotra

Reputation: 1106

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++;
    }
}

Upvotes: 0

JiteshW
JiteshW

Reputation: 2205

why you are not using reloadData ... I guess this will help you in what you want to display on your UI. If you use for loop and add one data into your actual array and at end if you call reload, it will give you that effect.

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

EDIT Try 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];
            }


        }

Upvotes: 0

borrrden
borrrden

Reputation: 33421

Move your start updates call to just before the insertRowsAtIndexPaths: call. See if that helps. I'm surprised this works at all because you call end update multiple times but start only once.

Actually this will probably still be so fast that you don't even notice. Look into NSTimer or GCD's dispatch_after to stagger the updates.

Upvotes: 1

Stavash
Stavash

Reputation: 14304

The for loop doesn't matter in this case - the OS accumulates all animation actions and commits them together. To do this right, you'll have to use animation stop selector and call an external method to add the next cell until all cells have been added.

Upvotes: 0

Related Questions