Anand Prakash
Anand Prakash

Reputation: 311

How to Append data on pull to refresh Tableview in ios?

How to append 20 data in every one go when I pull to refresh the table in ios.first time load 20 data then on pulltorefresh add 20 more data and so on.

Upvotes: 0

Views: 709

Answers (3)

Hitesh Surani
Hitesh Surani

Reputation: 13537

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView == scrollObj) {
        CGFloat scrollPosition = scrollObj.contentSize.height - scrollObj.frame.size.height - scrollObj.contentOffset.y;
        if (scrollPosition < 30)// you can set your value
        {
            //code here you want to do when refresh done
            //Suppose you have 20 record in arryTemp
            //arryTemp is used to populate data into table
             for(int i=0;i<arryTemp.count;i++){
                 arryTemp =[aryyTemp addObject(arryTemp  objectAtIndex(i)];
             }
             [tableView reloadData]
        }
    }
}

Upvotes: 1

gontovnik
gontovnik

Reputation: 700

The easiest way is to use already build component. You can try to use SVPullToRefresh. This repo contains both pull to refresh and infinite scroll.

Pull to refresh is used to reload existing data, or reset all data and pull only latest.

Infinite scroll is used to append data as you scroll.

If you need only infinite scroll then you can go for this one - UIScrollView-InfiniteScroll.

If you are interested and would like to have a look at more difficult pull to refresh (can be done something similar in infinite scroll as well). Then the best thing to look at would be, probably, CBStoreHouseRefreshControl.

Upvotes: 1

Rony Rozen
Rony Rozen

Reputation: 4047

That's not exactly how "pull to refresh" is supposed to work. It's supposed to actually refresh the data being displayed, and not be a replacement for "load more".

In order to achieve what you originally asked, all you need to do is keep an indication of how many elements you have loaded so far and every time the user pulls to refresh, just add the next 20 to the array and reload the table data.

However, what you should be doing, is implement "pull to refresh" just the way it was intended to be used, and add another logic for "load more" which will be called whenever the user scrolls all the way to the bottom of the table view. There, you can load 20 more elements and display them to the user.

Upvotes: 1

Related Questions