我无法弄清楚Parse(假设内置)分页是如何工作的。我做了几个小时的研究,几乎所有我发现的都是用Objective-C编写的,当我尝试将它转换为Swift时,应用程序无法运行。
我正在运行PFQueryTableViewController来加载MPMoviePlayerControllers中的视频。
首先,我不确定要放入numberOfRowsInSection
的内容。 Obarse-C中的Parse的AnyPic教程建议return objects.count * 2 + (self.paginationEnabled ? 1 : 0)
。坦率地说,我不知道第二部分是什么意思,但无论如何,如果我在numberOfRowsInSection
中返回任何,例如objects.count
,应用程序会立即崩溃。
如果我只是遗漏numberOfRowsInSection
,那么视频的第一页就完全成功了。然后,一旦我向下滚动,我会进入“加载更多...”单元格,该单元格由Parse自动添加。然后应用冻结了。在此次崩溃期间,日志只会读取(lldb)
。
我怀疑即使这个其他单元格正在出现,也没有加载单元格,但我再次确定该怎么做,因为我放入numberOfRowsInSection
会使应用程序崩溃。
接下来,我完全不知道放在cellForNextPageAtIndexPath
中的内容。 Parse的教程建议:
PAPLoadMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];
if (!cell) {
cell = [[PAPLoadMoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadMoreCellIdentifier];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
cell.hideSeparatorBottom = YES;
cell.mainView.backgroundColor = [UIColor clearColor];
}
return cell;
我无法在Swift中模拟此代码,特别是第三行。我试过运行应用程序,跳过if语句,但没有成功。 也许这种方法需要完整才能使分页起作用?
基本上就我的问题而言,这是我尝试解决这个问题的一些努力。
在cellForRowAtIndexPath
中,我尝试了以下内容:
if indexPath.row == objects.count - 1 { //Does NOT work if '-1' is omitted
//Extremely glitchy
println("Load next page")
loadNextPage()
}
这是中等成功的。当indexPath接近底部时,它实际上成功加载了更多页面。但是,它非常小问题。它偶尔崩溃,说fatal error: Array index out of range (lldb)
这似乎是我滚动得足够快以实际查看“加载更多...”单元格,所以它看起来像上面的问题一样。
如果有人可以帮助我,我将(几乎)永远感激。
修改:我在处理数据方面包含了一些代码。
以下是我对该表的查询:
override func queryForTable() -> PFQuery! { //Same code as when normally loaded videos
var query = PFQuery(className:"post")
query.orderByDescending("createdAt")
return query
}
“post”类的重要方面是videoFile和label。这些已成功加载到PFTableViewCell。
接下来,我如何处理cellForRowAtIndexPath:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
super.tableView.cellForRowAtIndexPath(indexPath)
let myCell = tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPath) as TableViewCell
println("Current indexPath: \(indexPath.row)")
if indexPath.row == objects.count - 1 {
println("Last current object")
//loadNextPage()
}
//Parse: Assigning values(url, user, count) to cells
let vidFile = object.valueForKey("videoFile") as PFFile
let vidStr = vidFile.url
let vidURL = NSURL(string: vidFile.url)
myCell.videoURL = vidURL
myCell.usernameLabel.text = object.valueForKey("user") as? String
return myCell
}
一切正常。有趣的是,当Parse中的“加载更多”单元格进入视图时,println("Current indexPath: \(indexPath.row)")
将不会打印任何内容,这会让我产生怀疑。
我有一些方法可以确保不加载新视频,除非单元占据视图的大部分。如果它试图在“加载更多”单元格中加载视频,这会导致崩溃,但我已经实施了检查以禁止此操作,并且当单元格进入视图时它仍然崩溃。
例如:
if indexPathToLoad.row != objects.count {
//Display video at proper indexPath
cell.displayVideo()
}
else { // Does not fire
println("Do not try to load new video: Load next page")
}