如何在SWIFT中推送视图之前加载视图?

时间:2015-04-11 10:24:19

标签: ios uitableview swift segue viewdidappear

我有一个表视图控制器,当我按下一个单元格时,我会在显示视图之前加载一些数据。加载数据时,我按下视图。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

  var post : Post = posts[indexPath.row] as Post
  self.selectedPost = post

  var alert:UIAlertController = ISAMContext.sharedInstance.alertDownloadInProgress("DOWNLOADING", sender:self)

   let request:NSURLRequest=NSURLRequest(URL:imageUrl)

   NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
            if error == nil {

                self.comments=self.api.getCommentsData(post)

                dispatch_async(dispatch_get_main_queue(), {
                    if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                        alert.dismissViewControllerAnimated(true, completion: {
                            self.performSegueWithIdentifier("postview", sender: self)
                        })
                    }
                })
            }
}


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   
        }
    }
}

我对UITextView的高度有些麻烦。我想根据字符串的长度调整高度(参见我的另一个问题:How to adjust the height of a textview to his content in SWIFT?

我找到了一种方法,在viewDidAppear中调用sizeThatFits:

override func viewDidAppear(animated: Bool) {

    let contentSize = postTextView.sizeThatFits(postTextView.bounds.size)
    var frame = postTextView.frame
    frame.size.height = contentSize.height
    postTextView.frame = frame
}

它可以工作,但是当我在viewDidAppear中调用我的代码时,我可以看到视图出现,然后更新textView的高度。

  1. 按一个单元格
  2. 加载数据
  3. 推送视图
  4. 显示视图
  5. 然后调整textview的高度 - >我可以看到高度变化
  6. 所以我的问题是:在推送之前可以完全加载视图(内容和显示)吗?

1 个答案:

答案 0 :(得分:3)

将您的代码放入viewWillAppear。这将在视图出现在屏幕上之前为您调用。