ios - 如何在推送 View Controller SWIFT 之前预加载所有 tableView 单元

标签 ios swift uitableview uialertcontroller

我有一个带有帖子列表的 UITableViewController。当我按下一个单元格时,我会在发布图像和发布评论下载期间显示一个警报 Controller 。然后关闭alertController并推送帖子的UIViewController(PostViewcontroller)。 PostViewcontroller 有一个用于这些评论的 UITableView。

目前我的行为:

  1. 按我的 UITableViewController 的一个单元格
  2. 显示警报 Controller
  3. 下载图片和评论
  4. 关闭alertController
  5. PostViewcontroller 调用 cellForRowAtIndexPath 帖子的每条评论。评论很多,所以需要一个 很多时间
  6. 最后 UITableViewController 推送 PostViewcontroller

在所有 cellForRowAtIndexPath 调用之后,我想在推送 PostViewcontroller 之前关闭警报 Controller 。

异常行为:

  1. 按我的 UITableViewController 的一个单元格
  2. 显示警报 Controller
  3. 下载图片和评论
  4. PostViewcontroller 调用 cellForRowAtIndexPath 帖子的每条评论。评论很多,所以需要一个 很多时间
  5. 关闭alertController
  6. 最后 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 加载其单元格。

您可以将行为更改为以下内容:

  1. 按下 UITableViewController 的一个单元格
  2. 推送 PostViewcontroller
  3. 显示alertController(从PostViewController内部)
  4. 下载图片和评论
  5. 告诉 PostViewController 的 tableView reloadData (它只会 加载填充显示所需的单元格,这样就不会 需要很多时间。)
  6. 关闭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/

相关文章:

ios - UIBlurEffect 仅在 StatusBar 上(不在 UINavigationBar 上)

iphone - 使用 UIRoundRectButton 在 iPhone 模拟器中剪切文本 - 与我在 Storyboard 中看到的不同

Swift 3 升级 : Type 'Dictionary<NSObject, AnyObject>?' has no subscript members

ios - 火力地堡 5 : Ambiguous use of 'observeSingleEvent'

swift - 编辑按钮没有任何功能,单元格左侧没有带减号的红色圆圈

iphone - UITableview titleForHeaderInSection 显示不正确

ios - UITableView 嵌套在 ContainerView 中不突出显示

ios - 如何使用预处理器有条件地声明新类的父类(super class)?

ios - 是否有一些 iOS 库在旧设备上模拟 3D 触摸查看和弹出?

arrays - Swift 4 排序有错误的元组