【问题标题】:How does one prevent labels of different Swift cells displaying on top of each other?如何防止不同 Swift 单元格的标签彼此重叠显示?
【发布时间】:2021-10-25 17:30:15
【问题描述】:

Screenshot of the double text label

Current constraints of the label

Complete Hirearchy

我目前的相关代码是什么

 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 和 A​​li 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


【解决方案1】:

我通过检查 label2 上的清晰图形上下文来使其工作。

【讨论】:

    【解决方案2】:

    我还没有在你的牢房课上见过。当您遇到约束问题时,就会发生该问题。由于自动布局系统无法计算行的高度,因此高度变为 0,从该点开始的任何更改都可能是错误的。

    我建议您检查单元格代码,将标签添加到 contentView(如果没有)并在它们之间添加约束:

    例如:

    bottomLabel.topAnchor.constraint(equalTo: topLabel.bottomAnchor, constant: 8).isActive = true
    

    还将两个标签的压缩阻力设置为 .defaultHigh,如果您有硬编码的行的高度,请将其删除。

    【讨论】:

    • 但它是一个标签而不是 2
    【解决方案3】:

    如果您检查单元高度,我认为您的问题会解决, 检查 heightForCellAtRow 函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 2017-02-01
      • 2013-08-14
      • 1970-01-01
      • 2012-11-11
      相关资源
      最近更新 更多