简体   繁体   English

如何在iOS上添加拉动数据以刷新Tableview?

[英]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. 为了实现您最初要求的功能,您需要做的是保持指示到目前为止已加载了多少个元素,并且每次用户拉动刷新时,只需将下20个元素添加到数组中并重新加载表数据即可。

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. 在那里,您可以再加载20个元素并将其显示给用户。

- (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 . 您可以尝试使用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 . 如果只需要无限滚动,则可以选择一个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 . 那么最好看的是CBStoreHouseRefreshControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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