Gaurav Taywade
Gaurav Taywade

Reputation: 8347

uitableview autoupadate while scrolling to top

I want to update Uitableview when it will be scroll to top. As in facebook app when we scroll uitableview to top it will show "upadating" and insert new rows in top of table view. I want to give same effect in my app. is it possible to design it using default uitableview methods or we need to customize it. If anyone is having idea about it please please reply.

Upvotes: 3

Views: 5749

Answers (3)

Manny
Manny

Reputation: 6297

You may want to detect when your user scrolled to top:

Several ways to do this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y == 0)
        NSLog(@"At the top");
}

Source: How can I get scrollViewDidScrollToTop to work in a UITableView?

Or using:

scrollViewDidScrollToTop: 

Source: http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollingViewContent/ScrollingViewContent.html

You can now fire your events depending on the user's actions: i.e. When user scrolled to top, call your UI change using, then call your update logic.

insertRowsAtIndexPaths:withRowAnimation:

Sample and Source: http://www.mlsite.net/blog/?p=466

After update logic has ended, call [tableView reloadData] assuming your datasource is now updated. Optionally you may want to make use of deleteRowsAtIndexPaths:withRowAnimation: to add effects when removing your cell notifying that you are currently updating.

Upvotes: 7

Trunal Bhanse
Trunal Bhanse

Reputation: 1691

Another way :

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
            [self fetchMoreMessages];
    }
}

Upvotes: 5

EmptyStack
EmptyStack

Reputation: 51374

You can use insertRowsAtIndexPaths: to insert new rows at the front of table view. For example,

 NSIndexPath *indexPathForRow_0_0 = [NSIndexPath indexPathForRow:0 inSection:0];
 NSIndexPath *indexPathForRow_0_1 = [NSIndexPath indexPathForRow:1 inSection:0];
 [yourTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathForRow_0_0, indexPathForRow_0_1, nil] withRowAnimation:UITableViewRowAnimationTop];

The above code inserts two rows at the index 0 and 1 in section 0.

Note: You need to customize your dataSource and delegate methods to adjust with these insertions. Important thing is you need to return the correct value from numberOfRowsInSection method. Here, in the above example, your numberOfRowsInSection method should return yourPreviousNumberOfRows + 2, because we added two rows here.

Upvotes: 2

Related Questions