【发布时间】:2021-10-25 17:30:15
【问题描述】:
Screenshot of the double text label
Current constraints of the label
我目前的相关代码是什么
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ViewControllerTableViewCell {
let immy = cell.viewWithTag(1) as? UIImageView
let person: Userx = people[indexPath.row]
let text = person.Education
cell.lblName.text = person.Education. ///key line
cell.lblName.text = text?.uppercased()
cell.lblName?.layer.masksToBounds = true
let person5 = colorArray[indexPath.row]
let person6 = colorArray1[indexPath.row]
let person7 = colorArray2[indexPath.row]
let like = cell.viewWithTag(3) as? UIButton
cell.backgroundColor = person5
like?.backgroundColor = person6
immy?.backgroundColor = person7
cell.lblName.baselineAdjustment = .alignCenters
cell.postID = self.people[indexPath.row].postID
cell.row = indexPath.row
cell.delegate = self
cell.delegate2 = self
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
like?.isUserInteractionEnabled = true
}
return cell
}
return UITableViewCell()
}
我尝试了什么:
为了重用,我试过lblName.text = "" and lblName.text = nil没用。
我也在 cellForRowAt 中尝试过:
cell.lblName.text = nil
if cell.lblName.text == nil{
cell.lblName.text = person.Education
}
我也试图通过约束来完成它,没有运气。
我认为可能是什么原因
在我从 TableViewController 场景更改为 ViewController(带有输出表)场景之前,这并没有发生。
在我将单元格设置为不同颜色之前,它也没有发生。
【问题讨论】:
-
看起来您有两个相互重叠的标签。你需要设置约束来防止这种情况发生——但你没有向我们展示你的单元格是如何布局的。
-
这是通过为每个视图设置约束来防止重叠
-
@DonMaf 和 Ali A. Jalil 我现在添加了当前约束和带有问题中标签的 VC 的层次结构。缺少什么约束?这些约束以前在我使用 TableViewController 时有效
-
@florida27 - 非常混乱...您的图像显示
label2,但您的代码引用.lblName...您还显示了几个按钮,但您的“屏幕截图”没有显示任何按钮...在设置.text = person.Education之前绝对不需要设置.text = nil...给我们看一个如下所示的屏幕截图:i.stack.imgur.com/Swg77.png,以便我们了解您在做什么。 -
@DonMag label2 的引用出口是 lblName-cell。对按钮感到抱歉,只有带有编辑名称的按钮处于活动状态。其他的只是半透明的按钮,不做任何事情。这是您的示例中未编辑的场景:link
标签: ios swift uitableview uilabel prepareforreuse