【问题标题】:How to load a view before pushing it in SWIFT?如何在 SWIFT 中推送视图之前加载视图?
【发布时间】:2015-04-11 10:24:18
【问题描述】:

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

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. THEN 调整 textview 的高度 -> 我可以看到高度变化

所以我的问题是:可以在推送之前完全加载视图(内容和显示)吗?

【问题讨论】:

    标签: ios uitableview swift segue viewdidappear


    【解决方案1】:

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

    【讨论】:

    • 我试过了,但是如果我这样做,textview的高度不会改变。
    • 并在 ViewWillAppear 中调用 [self view];以前?
    • 您在使用自动布局吗?如果是这样,请修改约束,而不是框架本身。
    • @JavierFloresFont,如何快速调用[self view]? self.view() 不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多