【问题标题】:UILabel not getting contents immediatelyUILabel 没有立即获取内容
【发布时间】:2015-11-19 02:09:45
【问题描述】:

我正在使用 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
}

投票栏的限制:

【问题讨论】:

  • 你能在你的cellForRowAtIndexPath 中显示代码吗?您的 Storyboard 中的标签是如何配置的?
  • @Paulw11 我添加了相关代码和配置选项
  • 认为他们想看到约束。
  • @beyowulf 也添加了这些。
  • 什么是 postImage?它的限制是什么?

标签: ios swift uitableview


【解决方案1】:

您需要确保您的 UI 更新发生在主线程中。我怀疑您的 API.getNew() 调用似乎是异步的,并且可能在辅助线程中回调该块? (我只是推测它)。确保 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 更新。

【讨论】:

  • 修复了模拟器中的代码,但没有修复硬件 iPhone 上的代码。奇怪的!请注意,我的 SIM 卡在 iOS 9 上,而我的 iPhone 在 iOS 8 上。
  • 我最终找到了this link,它告诉我在从 cellForRowAtIndexPath 返回之前添加 cell.layoutIfNeeded()。这终于在 iOS 8 上修复了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2023-03-07
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 2016-08-30
相关资源
最近更新 更多