为什么我的UitableViewCell中的多行uiLabels在第一次负载时会截断,但在滚动时纠正自己? 我对我的应用程序中的一个桌面视频遇到了问题,想知道是否有人遇到了类似的事情。我在自定义tableviewcell中有2个标签 - 一个QuestionLabel和一个答案标签 -

问题描述 投票:0回答:2
- 具有以下约束:

enter image description here 在两个Uilabels中,我都设置为0的行数,以使其成为多行,并将两者添加到这两个上,如下所示: preferredmaxLayoutWidthenter image description here 根据其他stackoverflow建议,我在cell.questionLabel.preferredMaxLayoutWidth = cell.questionLabel.frame.width cell.answerLabel.preferredMaxLayoutWidth = cell.answerLabel.frame.width

方法中也具有以下内容:

viewDidLoad()

在第一次加载时,即使线设置为0,并且在标签不一致的位置奇怪的是,标签仍在截断。但是,当将屏幕向下滚动然后返回到顶部时,标签自动校正自己,布局非常完美。请参阅下面的屏幕截图:

以前有人遇到过这个问题,如果可以的话,您可以将我指向正确的方向。我不确定这是我未能设置的东西,还是我的自动布局约束的问题。
    

enter image description here

ios swift uitableview autolayout uilabel
2个回答
2
投票
这样的约束。删除以下行

self.faqTableView.rowHeight = UITableViewAutomaticDimension self.faqTableView.estimatedRowHeight = 140.00 enter image description here 将Uilabel的行数设置为零,并实现以下方法

cell.questionLabel.preferredMaxLayoutWidth = cell.questionLabel.frame.width cell.answerLabel.preferredMaxLayoutWidth = cell.answerLabel.frame.width self.faqTableView.estimatedRowHeight = 140.00 enter image description here

如果这些修复程序都不适合您,那么您可能不会在主线程上填充文字标签。

示例(解决方案):

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

示例(问题):

func configureForCell(message: Messages) {
    let auth = message.auth
    let authRef = db.collection("Users").document(auth)
    authRef.getDocument { document, error in
        if let error = error {
            print("Error fetching document: \(error.localizedDescription)")
            return
        }
        guard let document = document, document.exists,
              let data = document.data(),
              let name = data["name"] as? String,
              let imageUrl = data["img"] as? String else {
            return
        }
        self.nameLabel.text = name
        self.thumbnail.loadImageUsingCacheWithUrlString(urlString: imageUrl)
    }
    self.textLabel.text = message.text // Populating your label ensures it happens on the main thread and the character count is considered on initial view load
}

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.