我有一个带有帖子列表的 UITableViewController。当我按下一个单元格时,我会在发布图像和发布评论下载期间显示一个警报 Controller 。然后关闭alertController并推送帖子的UIViewController(PostViewcontroller)。 PostViewcontroller 有一个用于这些评论的 UITableView。
目前我的行为:
- 按我的 UITableViewController 的一个单元格
- 显示警报 Controller
- 下载图片和评论
- 关闭alertController
- PostViewcontroller 调用 cellForRowAtIndexPath 帖子的每条评论。评论很多,所以需要一个 很多时间
- 最后 UITableViewController 推送 PostViewcontroller
在所有 cellForRowAtIndexPath 调用之后,我想在推送 PostViewcontroller 之前关闭警报 Controller 。
异常行为:
- 按我的 UITableViewController 的一个单元格
- 显示警报 Controller
- 下载图片和评论
- PostViewcontroller 调用 cellForRowAtIndexPath 帖子的每条评论。评论很多,所以需要一个 很多时间
- 关闭alertController
- 最后 UITableViewController 推送 PostViewcontroller
在我的 UITableViewController 中:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var post : Post = posts[indexPath.row] as Post
//Display the alert controller during the image downloading
var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
// If the image does not exist, we need to download it
var imageUrl: NSURL! = NSURL(string: post.attachment!.imageUrl)
// Download an NSData representation of the image at the URL
let request:NSURLRequest=NSURLRequest(URL:imageUrl)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
var image = UIImage(data: data)
CoreDataManager.sharedManager.addImageToCoreData(post, image:image!, type:"full")
//download the comments
self.comments=self.api.getCommentsData(post)
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
//Image and comments downloaded, dismiss the alertcontroller
self.dismissViewControllerAnimated(true, completion: {
self.performSegueWithIdentifier("postview", sender: self)
})
}
})
}
})
}
我使用prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "postview" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let destination = (segue.destinationViewController as! UINavigationController).topViewController as! PostViewController
destination.post=selectedPost!
destination.comments=comments!
destination.delegate=self
self.splitViewController?.toggleMasterView()
}
}
}
我尝试在prepareForSegue中关闭alertController,但出现此错误:
pushViewController:animated: called on <UINavigationController 0x78e43070> while an existing transition or presentation is occurring; the navigation stack will not be updated.
我不知道该怎么做以及是否可能。我错过了一些东西,但是什么?
最佳答案
在 View Controller 本身加载之前,您不能强制 View Controller 的 TableView 加载其单元格。
您可以将行为更改为以下内容:
- 按下 UITableViewController 的一个单元格
- 推送 PostViewcontroller
- 显示alertController(从PostViewController内部)
- 下载图片和评论
- 告诉 PostViewController 的 tableView
reloadData
(它只会 加载填充显示所需的单元格,这样就不会 需要很多时间。) - 关闭alertController
另一种选择是让每个 tableViewCell 下载自己的图像和评论。它可以在下载时显示事件指示器,然后在下载完成后显示数据。
Apple 有示例代码来演示这一点:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
关于ios - 如何在推送 View Controller SWIFT 之前预加载所有 tableView 单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359456/