UILabel没有立即获取内容

时间:2015-11-19 02:09:46

标签: ios swift uitableview

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

标签在故事板中配置为:

初始加载:

滚动后:

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
}

投票栏的限制:

1 个答案:

答案 0 :(得分:0)

您需要确保在主线程中进行UI更新。我怀疑你的API.getNew()调用似乎是异步的,并且可能在辅助线程中调用该块? (我只是猜测它)。确保在主线程中进行UI更新。这是对代码的更改 -

var text = "I'm a little tea pot"

text = text.split(" ")
//["I'm", "a", "little", "tea", "pot"]

final = $.map(text, function(val,i){
 return val = val[0].toUpperCase() + val.slice(1,val.length)
})
// ["I'm", "A", "Little", "Tea", "Pot"]

final.join(" ")
// "I'm A Little Tea Pot"

同样请注意代码中的这种模式,并确保您不会从辅助线程运行UI更新。