UILabel没有立即获取内容

我使用UITableViewAutomaticDimension让我的TableViewCells自动resize的内容。 如果我在第一个单元格中有多行文本,它将采用正确的高度,但只显示第一行内容,直到我向下滚动并备份。

故事板中的标签configuration如下:

初始加载:

滚动后:

ViewDidAppear:

override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.tableView!.estimatedRowHeight = 250 self.tableView!.rowHeight = UITableViewAutomaticDimension ProgressHUD.show("Loading...", interaction: false) let API = postAPI() API.getNew() { (result: [Post]?) in ProgressHUD.dismiss() if let ps = result { if self.posts.count != ps.count { self.posts.removeAll() self.posts.appendContentsOf(ps) self.tableView.reloadData() } } else { ProgressHUD.showError("There was a problem getting new posts") } } } 

的cellForRowAtIndexPath:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("OMCFeedCell", forIndexPath: indexPath) as! OMCFeedTableViewCell let p = posts[indexPath.row] cell.selectionStyle = .None let data = p.content!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) let content = try! JSON(NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) if let image = content["image"].string { let uploads = uploadAPI() uploads.tryGetImage(image) { (result: UIImage?) in if let i = result { cell.postImage.contentMode = .ScaleAspectFit cell.postImage.image = i } } } cell.postText.text = "" if let text = content["text"].string { cell.postText.text = text } return cell } 

投票栏限制条件:

您将需要确保您的UI更新发生在主线程中。 我怀疑你的API.getNew()调用似乎是asynchronous的,可能该块在辅助线程中被callback? (我只是在猜测)。 确保UI更新在主线程中发生。 做这个改变的代码 –

 API.getNew() { (result: [Post]?) in //Ensure to run UI updates in main thread. The ProgressHUD.dismiss(), //self.tableView.reloadData() must be run in main thread.. dispatch_async(dispatch_get_main_queue(),{ if let ps = result { ProgressHUD.dismiss() if self.posts.count != ps.count { self.posts.removeAll() self.posts.appendContentsOf(ps) self.tableView.reloadData() } } else { ProgressHUD.showError("There was a problem getting new posts") } }) } 

同样,在代码中寻找这种模式,并确保不要从辅助线程运行UI更新。