【发布时间】:2013-02-03 05:55:54
【问题描述】:
是否可以在UITableView 的底部添加UIRefreshControl?
我会用它来加载更多数据。
请问有什么建议吗?
【问题讨论】:
标签: uitableview ios6 uirefreshcontrol
是否可以在UITableView 的底部添加UIRefreshControl?
我会用它来加载更多数据。
请问有什么建议吗?
【问题讨论】:
标签: uitableview ios6 uirefreshcontrol
我相信这个问题不会有任何简单的解决方案。可能有人可以编写一个单独的库来实现此行为,而且一旦您在 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];
}
}
或者你可以这样做:
- (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
}
}
}
您一定已经在我上面描述的许多问题中看到过这样的方法,当然不是您所要求的。但是您可以做的是,在上述代码加载更多数据的过程中,您可以添加一个子视图并显示一些类似于 UIRefreshControl 提供的图像。在子视图中添加图像以显示为进度,直到执行上述代码。回家这有帮助。
顺便说一句,我建议你不要这样做,因为它只会浪费你的时间来制作如此小的东西,除非你只是为了学习目的而这样做。
【讨论】:
以下答案在 Xcode 8 和 Swift 3 中。
先声明
var spinner = UIActivityIndicatorView()
比在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
现在终于override scrollViewDidEndDragging 委托方法具有以下内容:
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()
}
}
【讨论】:
您可以使用 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;
隐藏它: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
}
}
在向 tableView 添加数据之前,将当前偏移量保存为CGPoint offset = tableView.contentOffset;
调用 reloadData 然后将之前保存的偏移设置回 tableView [tableView setContentOffset:offset animated:NO];
这样你就可以感觉到底部添加了新数据。
【讨论】:
tableFooterView 设置为UIRefreshControl 视图
UIActivityIndicatorView 不是 UIRefreshControl!
我最近遇到了同样的问题,并为 UIScrollView 类编写了一个类别。这里是CCBottomRefreshControl。
【讨论】:
实际上,免费的 Sensible TableView 框架确实提供了开箱即用的功能。您指定批号,它将自动获取数据,将最后一个单元格显示为“加载更多”单元格。单击该单元格后,它将自动加载下一批,依此类推。值得一看。
【讨论】:
对于 UITableView,我建议采用以下方法:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// if this is the last row that we have in local data
// and we didn't reach the total count of rows from the server side
if (count == indexPath.row+1 && count < total)
{
// .. fetch more rows and reload table when data comes
}
}
我没有故意使用滚动视图方法,因为滚动视图很慢。碰巧滚动视图滚动,我们请求更多数据,刷新更多行,滚动视图尚未完成滚动,它仍在减速并导致获取更多数据时出现问题。
【讨论】:
【讨论】:
在 cocoapods 中使用 CCBottomRefreshControl,
因为它是语言目标。 您可以将 pod 文件添加到您的 swift 项目中,然后运行它, 我已经做到了,对我来说没有问题
希望对你有帮助。
【讨论】: