【问题标题】:Text overlapping itself in table cells SWIFT文本在表格单元格中重叠自身 SWIFT
【发布时间】:2015-07-09 14:11:43
【问题描述】:

我有一些文本进入我在表格视图单元格中创建的 UIlabels。当这些表格视图单元格更新时,文本会自行重叠,几乎就像之前的文本一样没有被删除,如下所示:

代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

 let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! UITableViewCell

    var nameLabel = UILabel(frame: CGRectMake(cell.frame.size.width * 0.040, cell.frame.size.height * 0.22, cell.frame.size.width * 0.735, cell.frame.size.height * 0.312))

    var userAtIndexPath = finalMatchesBlurUser[indexPath.row]

    nameLabel.text = userAtIndexPath.username.uppercaseString

    cell.addSubview(nameLabel)
}

finalMatchesBlurUser 是从 Parses 数据库中获取的一个 PFUser,它将发生变化,而这种变化是导致名称重叠的原因。

谁能指出为什么会这样?

【问题讨论】:

    标签: swift uitableview parse-platform


    【解决方案1】:

    每次更新 tableview 时,它都会检查队列以查看它是否可以重用一个单元格而不是初始化一个新单元格。在这种情况下,当它更新时,它在队列中有单元格,因此每次表更新时都会添加一个新的标签子视图,这会导致这种效果。在这种情况下,您应该只在标签子视图不存在的情况下添加它。否则,只需更新该子视图的文本即可。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
         let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! UITableViewCell
    
             if let nameLabel = cell.viewWithTag(100) as? UILabel{
    
                  var userAtIndexPath = finalMatchesBlurUser[indexPath.row]
    
                  nameLabel.text = userAtIndexPath.username.uppercaseString
             }
             else{
                   nameLabel = UILabel(frame: CGRectMake(cell.frame.size.width * 0.040, cell.frame.size.height * 0.22, cell.frame.size.width * 0.735, cell.frame.size.height * 0.312))
    
                   nameLabel.tag = 100;
    
                   var userAtIndexPath = finalMatchesBlurUser[indexPath.row]
    
                   nameLabel.text = userAtIndexPath.username.uppercaseString
    
                   cell.addSubview(nameLabel)
             }
         return cell;
         }
    

    【讨论】:

    • 谢谢,现在明白一点了。有没有办法检查 UILabel 是否已经生成,比如使用 if 语句?
    • 根据您的操作方式,您可以遍历单元格的子视图并检查标签是否存在,如果存在,只需更改文本。如果没有,请创建标签并为其分配标签值。当您最初循环遍历单元格子视图以检查其存在时,该标记值将是您检查的内容。有意义吗?
    • 一种,标记 UILabel 和 if 语句通过标记检查它?我不能从视图 removeFromSubview 中删除 nameLabel
    • 我的也只是更改文本内容而不是删除它只是为了重新添加它。希望我的编辑可以澄清我为您的示例考虑的逻辑。
    • 对不起,还有一个问题,这似乎只适用于 nameLabel,但正如您在图片中看到的那样,我还有 2 个标签,它们没有按预期工作?
    【解决方案2】:

    即使重用单元格,每次都会创建 UILabel。 一种解决方案是在 Interface Builder 中创建 UILabel 并分配一个标签(例如 100)。

    然后使用此代码

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! UITableViewCell
        let nameLabel = cell.viewWithTag(100) as! UILabel
        let userAtIndexPath = finalMatchesBlurUser[indexPath.row]
        nameLabel.text = userAtIndexPath.username.uppercaseString
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多