iOS 中 uiscrollview 的底部刷新控件



如何在UIScrollView的底部添加UIRefreshController。我正在UIScrollView中实现加载更多选项,以便我可以将更多数据加载到滚动视图中。

提前致谢...

将数据放入表视图中,然后可以使用标准UITableViewDelegate回调轻松完成此操作。不要试图操纵Apple自己的UIRefreshControl进入表格或滚动视图的底部,因为它会以麻烦告终。

只需这样做:

在您的tableView:numberOfRowsInSection:返回data.count+1

在您的tableView:cellForRowAtIndexPath:检查if (indexPath.row == data.count)。如果是这样,请创建并返回一个基本单元格,其文本标签设置为"加载更多..."

在您的tableView:didSelectRowAtIndexPath:再次检查if (indexPath.row == data.count)。如果是这样,请触发您自己的 [self loadMoreData] 方法,然后调用 [self.tableView reloadData]

我做了这样的事情来处理在表视图(即数据表)中加载更多数据:

1)在表格视图的底部添加一个活动指示器(即微调器)。

2)这样做:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    if(self.dataTable.contentOffset.y<0){
        //it means table view is pulled down like refresh
        return;
    }
    else if(self.dataTable.contentOffset.y >= (self.dataTable.contentSize.height - self.dataTable.bounds.size.height)) {
        NSLog(@"bottom!");
        self.spinner.hidden = false;
        [self.spinner startAnimating];
        [self refreshPulled];
    }
}
-(void)refreshPulled
{
    //get more data and reload table here and hide spinner when all is done
}

希望这对你有帮助。

UIRefreshControl 只能在 UITableView 中使用,正如它在 Apples 文档中所说:

Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behaviour.

最新更新