文本在表格单元格SWIFT中重叠 [英] Text overlapping itself in table cells SWIFT

查看:80
本文介绍了文本在表格单元格SWIFT中重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表视图单元格中创建的UIlabel中包含一些文本.当这些表格视图单元格更新时,文本本身就会重叠,就像以前的文本一样,这里的内容也没有被删除,就像这样:

I have some text which goes into UIlabels I have created inside a table view cell. When these table view cells become updated the text overlaps itself, almost like the previous text what was there has not been removed, like so:

代码:

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,该用户将发生更改,当更改导致名称重叠时.

The finalMatchesBlurUser is a PFUser fetched from Parses database that will be changing, when this change is what causes the names to overlap.

有人能指出为什么会这样吗?

Can anyone point out why this is happening?

推荐答案

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

Everytime you update the tableview, it checks the queue to see if it can reuse a cell instead of initializing a new one. In this case, when it updates, it has cells in the queue so you're adding a new label subview everytime the table updates which is causing this effect. In this case, you should only add the label subview if it doesn't exist already. Otherwise, just update the text of that subview.

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;
     }

这篇关于文本在表格单元格SWIFT中重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆