简体   繁体   中英

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

当我在ios中拉动刷新表时,如何每次添加20个数据。第一次加载20个数据,然后在pulltorefresh上添加20个数据,依此类推。

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.

- (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]
        }
    }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM