当用户滚动到最后一个 tableViewCell 时执行操作
Do action when user scrolls to last tableViewCell
上下文
- 建立一种新闻源
问题
- 我需要在用户滚动到最后一个 table 视图单元格时加载更多帖子
- 我不知道从哪里开始,如何实现,在代码中的什么位置
- 所以基本上我需要在用户滚动到最后一个 table 视图时调用服务器下载帖子,将这些帖子附加到当前 table 视图的底部
这是相关的代码片段(我认为!)
class GeneralPostAreaController: UIViewController {
...
extension GeneralPostAreaController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PostsCollection.postsFromParse.posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell
cell.postImage.image = PostsCollection.postsFromParse.posts[indexPath.row].image
cell.postName.text = PostsCollection.postsFromParse.posts[indexPath.row].name
cell.postText.text = PostsCollection.postsFromParse.posts[indexPath.row].text
cell.loadAudio = PostsCollection.postsFromParse.posts[indexPath.row].audioObject.parseAudioData
return cell
}
}
这是我用来从服务器获取数据的代码 (Parse)。请原谅可怕的数据结构,这是我的第一个应用程序!
class ParseQueryer: NSObject{
//HAVE TO RESTRUCTURE
var posts: [Post] = []
//this is not a very elegant way to reload table Data
func getPosts(selectedTableView: UITableView){
var query = PFQuery(className: "Post")
query.addDescendingOrder("createdAt")
query.limit = 10
query.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in
self.posts = result as? [Post] ?? []
selectedTableView.reloadData()
for post in self.posts{
post.name = post["Name"] as? String
post.text = post["Text"] as? String
//println(post.text)
if let audioData = post["Audio"] as? PFFile {
audioData.getDataInBackgroundWithBlock({
(audioData: NSData?, error: NSError?) -> Void in
if (error == nil){
post.audioObject.parseAudioData = audioData
}
})
}
if let imgData = post["Photo"] as? PFFile {
imgData.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil){
let image = UIImage(data: imageData!)
post.image = image
}
})
}
}
}
selectedTableView.reloadData()
我查看了答案 here,但它对我来说没有意义。
因此 UITableView
继承自 UIScrollView
因此您可以使用 scrollview 委托方法知道何时滚动到底部。
override func scrollViewDidScroll(scrollView: UIScrollView) {
//1. decide on the distance from the bottom that if the user scrolls to that point you will load more results
let minimumTrigger = scrollView.bounds.size.height + self.triggerDistanceFromBottom
// 2. Now you are checking to see that the height of all the content in the scrollview/tableView.( i.e. all the cells hights stacked ontop of eachother) is greater than the minimum height for triggering a load
// This step is so that if you have only a few results, the loadMoreResults method won't constantly be called.
if scrollView.contentSize.height > minimumTrigger {
//3. This calculated the distance from the bottom of the scrollview.
let distanceFromBottom = scrollView.contentSize.height - (scrollView.bounds.size.height - scrollView.contentInset.bottom) - scrollView.contentOffset.y
//4. then this is the crucial check.
if distanceFromBottom < self.scrollTriggerDistanceFromBottom {
// Do your stuff
}
}
}
您可以通过这种方法加载更多操作,因为 table 视图委托是滚动视图委托:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height)
{
// we are at the end
if(currentCount<pageCount)
{
if(isLoading)
{
return;
}
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
spinner.frame = CGRectMake(0, 22, 50, 44);
self.tblCentreEvents.tableFooterView = spinner;
isLoading=TRUE;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self populateDataInList];
});
}
}
}
最好的方法是测试屏幕底部的一个点,并在用户滚动时调用此方法 (scrollViewDidScroll
):
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
测试屏幕底部附近的一个点,然后使用它的 indexPath returns 检查 indexPath 是否是最后一行,如果是,则添加行。
上下文
- 建立一种新闻源
问题
- 我需要在用户滚动到最后一个 table 视图单元格时加载更多帖子
- 我不知道从哪里开始,如何实现,在代码中的什么位置
- 所以基本上我需要在用户滚动到最后一个 table 视图时调用服务器下载帖子,将这些帖子附加到当前 table 视图的底部
这是相关的代码片段(我认为!)
class GeneralPostAreaController: UIViewController {
...
extension GeneralPostAreaController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PostsCollection.postsFromParse.posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell
cell.postImage.image = PostsCollection.postsFromParse.posts[indexPath.row].image
cell.postName.text = PostsCollection.postsFromParse.posts[indexPath.row].name
cell.postText.text = PostsCollection.postsFromParse.posts[indexPath.row].text
cell.loadAudio = PostsCollection.postsFromParse.posts[indexPath.row].audioObject.parseAudioData
return cell
}
}
这是我用来从服务器获取数据的代码 (Parse)。请原谅可怕的数据结构,这是我的第一个应用程序!
class ParseQueryer: NSObject{
//HAVE TO RESTRUCTURE
var posts: [Post] = []
//this is not a very elegant way to reload table Data
func getPosts(selectedTableView: UITableView){
var query = PFQuery(className: "Post")
query.addDescendingOrder("createdAt")
query.limit = 10
query.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in
self.posts = result as? [Post] ?? []
selectedTableView.reloadData()
for post in self.posts{
post.name = post["Name"] as? String
post.text = post["Text"] as? String
//println(post.text)
if let audioData = post["Audio"] as? PFFile {
audioData.getDataInBackgroundWithBlock({
(audioData: NSData?, error: NSError?) -> Void in
if (error == nil){
post.audioObject.parseAudioData = audioData
}
})
}
if let imgData = post["Photo"] as? PFFile {
imgData.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil){
let image = UIImage(data: imageData!)
post.image = image
}
})
}
}
}
selectedTableView.reloadData()
我查看了答案 here,但它对我来说没有意义。
因此 UITableView
继承自 UIScrollView
因此您可以使用 scrollview 委托方法知道何时滚动到底部。
override func scrollViewDidScroll(scrollView: UIScrollView) {
//1. decide on the distance from the bottom that if the user scrolls to that point you will load more results
let minimumTrigger = scrollView.bounds.size.height + self.triggerDistanceFromBottom
// 2. Now you are checking to see that the height of all the content in the scrollview/tableView.( i.e. all the cells hights stacked ontop of eachother) is greater than the minimum height for triggering a load
// This step is so that if you have only a few results, the loadMoreResults method won't constantly be called.
if scrollView.contentSize.height > minimumTrigger {
//3. This calculated the distance from the bottom of the scrollview.
let distanceFromBottom = scrollView.contentSize.height - (scrollView.bounds.size.height - scrollView.contentInset.bottom) - scrollView.contentOffset.y
//4. then this is the crucial check.
if distanceFromBottom < self.scrollTriggerDistanceFromBottom {
// Do your stuff
}
}
}
您可以通过这种方法加载更多操作,因为 table 视图委托是滚动视图委托:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height)
{
// we are at the end
if(currentCount<pageCount)
{
if(isLoading)
{
return;
}
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
spinner.frame = CGRectMake(0, 22, 50, 44);
self.tblCentreEvents.tableFooterView = spinner;
isLoading=TRUE;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self populateDataInList];
});
}
}
}
最好的方法是测试屏幕底部的一个点,并在用户滚动时调用此方法 (scrollViewDidScroll
):
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
测试屏幕底部附近的一个点,然后使用它的 indexPath returns 检查 indexPath 是否是最后一行,如果是,则添加行。