是否可以将UIRefreshControl
添加到UITableView
的底部?我想用它来加载更多数据。请问有什么建议吗?
是否可以将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提供的图片。将这些图片添加到子视图中,以作为进度展示,直到上述代码执行完毕。希望这可以帮助到你。
顺便说一句,我建议您不要这样做,除非您只是出于学习目的而进行这样的操作,否则这只会浪费您的时间,因为这个东西太小了。
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
现在最终使用以下内容覆盖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* 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];
这样你就可以感受到新添加的数据出现在底部了。
UIActivityIndicatorView
而不是 UIRefreshControl
! - hunteros最近我遇到了同样的问题,为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,
由于它是Objective语言的,您可以将pod文件添加到Swift项目中,然后运行它, 我已经这样做了,对我来说没有任何问题
希望能有所帮助。