可以在
UIRefreshControl
底部添加UITableView
吗?
我会用它来加载更多数据。
请问,有什么建议吗?
我相信这个问题不会有任何简单的解决方案。也许有人可以编写一个单独的库来实现此行为,而且一旦您在表视图中缓存数据,它会导致更多复杂性。
但是让我们分解这个问题,这可能会帮助您实现您想要的目标。我已使用以下代码在应用程序中添加更多行:
- (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 来自定义底部的刷新控件。创建 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];
这样你就能感觉到底部有新的数据添加。
我最近遇到了同样的问题,并为 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,
希望有帮助。