UITefresh控件位于UITableView iOS6的底部?

Dan*_*any 25 uitableview ios6 uirefreshcontrol

是否有可能添加UIRefreshControl到底部UITableView?我会用它来加载更多数据.请问,有什么建议吗?

Jes*_*ica 29

我相信这个问题没有任何简单的解决办法.可能有人可能会编写一个单独的库来实现此行为,并且一旦在tableview中缓存数据,它将导致更多复杂化.

但是,让我们分解问题,这可能会帮助您实现您想要的目标.我使用以下代码在应用中添加更多行:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height)
    {
        NSLog(@"Scroll End Called");
        [self fetchMoreEntries];
    }
}
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
    {
        if (!self.isMoreData)
        {
            self.MoreData = YES;

            // call some method that handles more rows
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你必须在上面描述的许多问题中看到过这样的方法,当然不是你要求的.但是你可以做的是在上面的代码加载更多数据的过程中,你可以添加一个子视图并显示一些类似于UIRefreshControl提供的图像.在子视图中添加图像以显示为进度,直到执行上述代码.回家这有帮助.

顺便说一句,我建议你不要这样做,因为它只会浪费你的时间来制作如此小的东西,除非你只是为了学习目的而做.


Rah*_*ane 10

您可以使用UIView在底部自定义您的refreshControl.创建UIView并将其作为footerView添加到UITableView.

UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

[footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"refreshImage.png"]]];

tableView.tableFooterView = footerView;
Run Code Online (Sandbox Code Playgroud)

把它藏起来: tableView.tableFooterView.hidden = YES;

实现UIScrollView委托 -

(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
    {
             tableView.tableFooterView.hidden = NO;
            // call method to add data to tableView
    }

}
Run Code Online (Sandbox Code Playgroud)

在向tableView添加数据之前保存当前偏移量 CGPoint offset = tableView.contentOffset;

调用reloadData然后将以前保存的偏移量设置回tableView [tableView setContentOffset:offset animated:NO];

这样您就可以感觉到底部添加了新数据.


小智 8

我最近一直遇到同样的问题并为UIScrollView类编写一个类别.这是CCBottomRefreshControl.


Cho*_*war 7

以下答案是在Xcode 8和Swift 3中。

首先声明

var spinner = UIActivityIndicatorView()
Run Code Online (Sandbox Code Playgroud)

比在viewDidLoad方法中编写以下代码:

spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
spinner.stopAnimating()
spinner.hidesWhenStopped = true
spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60)
tableView.tableFooterView = spinner
Run Code Online (Sandbox Code Playgroud)

现在终于有了下面overridescrollViewDidEndDragging委托方法:

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let offset = scrollView.contentOffset
        let bounds = scrollView.bounds
        let size = scrollView.contentSize
        let inset = scrollView.contentInset

        let y = offset.y + bounds.size.height - inset.bottom
        let h = size.height

        let reloadDistance = CGFloat(30.0)
        if y > h + reloadDistance {
            print("fetch more data")
            spinner.startAnimating()
        }
}
Run Code Online (Sandbox Code Playgroud)

  • 伙计,你救了我的一天。这是Swift 3的最佳解决方案 (2认同)