上下文
- 构建一种新闻源
问题
- 我需要在用户滚动到最后一个表格 View 单元格时加载更多帖子
- 我不知道从哪里开始,如何实现,在代码中的什么位置
- 所以基本上我需要在用户滚动到最后一个表格 View 时调用服务器下载帖子,将这些帖子附加到当前表格 View 的底部
这是相关的代码片段(我认为!)
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 委托(delegate)方法来知道你何时滚动到底部。
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
}
}
}
关于ios - 当用户滚动到最后一个 tableViewCell 时执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531127/