如何在推送视图控制器SWIFT之前预加载所有tableView单元格

时间:2015-07-11 16:44:04

标签: ios swift uitableview uialertcontroller

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

目前我的行为是:

  1. 按下我的UITableViewController的单元格
  2. 显示alertController
  3. 下载图片和评论
  4. 关闭alertController
  5. PostViewcontroller调用cellForRowAtIndexPath 每个帖子的评论。有很多评论,所以它需要一个 很多时间
  6. 最后UITableViewController推送PostViewcontroller
  7. 我想在调用cellForRowAtIndexPath的所有调用之后,在推送PostViewcontroller之前解除alertController。

    例外行为:

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

      我不知道该怎么做以及是否可能。我错过了什么,但是什么?

1 个答案:

答案 0 :(得分:0)

在视图控制器本身加载之前,您无法强制视图控制器的表格视图加载它的单元格。

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

  1. 按下我的UITableViewController的单元格
  2. 推送PostViewcontroller
  3. 显示alertController(来自PostViewController内部)
  4. 下载图片和评论
  5. 告诉PostViewController的tableView到reloadData(它只会 加载填充显示所需的单元格,使其不受影响 花很多时间。)
  6. 关闭alertController
  7. 另一种选择是让每个tableViewCell下载自己的图像和评论。它可以在下载过程中显示活动指示器,然后在下载完成后显示数据。

    Apple提供了示例代码,用于演示:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

相关问题