滚动到底部时将行添加到UITableView
当用户滚动到UITableView
的底部时,有没有办法触发一个事件,比如用IBAction
? 如果发生这种情况,我想添加更多行。 我该怎么做呢?
只要听scrollViewDidScroll:
委托方法,比较内容偏移量与当前可能的偏移量,如果低于那么一些阈值调用您的方法来更新tableview。 不要忘记调用[tableView reloadData]
来确保它重新加载新添加的数据。
编辑:放在一起抽象的代码,不知道它是否工作,但应该。
- (void)scrollViewDidScroll: (UIScrollView *)scroll { // UITableView only moves in one direction, y axis CGFloat currentOffset = scroll.contentOffset.y; CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height; // Change 10.0 to adjust the distance from bottom if (maximumOffset - currentOffset <= 10.0) { [self methodThatAddsDataAndReloadsTableView]; } }
除非有点晚,但我想我find了一个更好的解决scheme:
代替
- (void)scrollViewDidScroll: (UIScrollView)scroll
我用了
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
这样更方便,导致事件只触发一次。 我在我的应用程序中使用这个代码来加载更多的行在我的tableview底部(也许你认识到这种types的重新加载从facebook应用程序 – 唯一的区别,他们在顶部更新)。
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { NSInteger currentOffset = scrollView.contentOffset.y; NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height; if (maximumOffset - currentOffset <= -40) { NSLog(@"reload"); } }
希望任何人都会帮助这个。
我使用这个片段。 在tableView:willDisplayCell:forRowAtIndexPath:
我检查,如果具有最后一个索引path的单元格即将diplayed。
对于一个部分的tableView:
[indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:0]
与更多部分:
[indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:[self numberOfSectionsInTableView:self.tableView]-1]
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if(!_noMoreDataAvailable) { if ([indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:0]]) { [self.dataSourceController fetchNewData]; } } }
获取dataSourceController后会通知tableView委托,这将重新加载数据。
NSInteger currentOffset = scrollView.contentOffset.y; NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height; // Hit Bottom? if ((currentOffset > 0) && (maximumOffset - currentOffset) <= 10) { // Hit Bottom !! }
这可以通过更简单的方式来实现,我猜你只需要确定滚动方向,就像在这种情况下用户向下滚动一样。
首先在你的.h文件中声明一个variables:
CGPoint pointNow;
在.m文件中,在scrollViewDidScroll方法中我们需要实现这个代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y > pointNow.y) { //Enter code here } }
迅速
这里是@ user944351的答案的Swift版本:
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { let currentOffset = scrollView.contentOffset.y let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height if maximumOffset - currentOffset <= -40 { print("reload") } }
只需将此方法添加到您的UITableView
委托类。 我发现-40
太大,但你可以根据你的需要进行调整。
更多信息
- 看到我的更完整的答案 ,有一个方法来重新加载数据使用数据库的限制和偏移的例子。
我不知道为什么你想这样做,但我想你可以添加一些代码到你的cellForRowAtIndexPath
方法,这样如果indexPath.row ==最后一行,调用方法添加更多的行…