0

I have a table view controller, when I press a cell, I load some data before displaying the view. When data are loaded, I push the view.

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

I have some troubles with the height of an UITextView. I would like adjust the height depending on the length of the string (see my other question: How to adjust the height of a textview to his content in SWIFT?)

I found a way, calling sizeThatFits in viewDidAppear:

override func viewDidAppear(animated: Bool) {

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

It works, but as I call my code in viewDidAppear, I can see the view appears THEN the height of textView is updated.

  1. Press on a cell
  2. Load data
  3. Push the view
  4. Display the view
  5. THEN adjust the height of the textview -> I can see the height change

So my question is : It's possible to completely load a view (content and display) before pushing it ?

Community
  • 1
  • 1
cmii
  • 3,556
  • 8
  • 38
  • 69

1 Answers1

3

Put your code in viewWillAppear. This will be called for you just before the view appears on screen.

Rengers
  • 14,911
  • 1
  • 36
  • 54