【问题标题】:TableViewCell overlaps textTableViewCell 与文本重叠
【发布时间】:2015-06-15 23:26:27
【问题描述】:

我的 TableViewCell 有问题

我的故事板中有两种类型的单元格。 当我滚动时,文本在某些单元格中重叠。我尝试了一切,但我不知道该怎么做。非常感谢您的帮助

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

        var storeNew = systemsBlog.getStore(listNews[indexPath.row].getIdStore())
        var newNotice = listNews[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell

                cell!.nameLabel.text = storeNew.getName()
                cell!.postLabel?.text = newNotice.getText()
                cell!.postLabel?.numberOfLines = 0
                cell!.dateLabel.text = newNotice.getDate()
                cell!.typeImageView?.tag = indexPath.row;
                return cell!
}



class TimelineCell : UITableViewCell {

    @IBOutlet var nameLabel : UILabel!
    @IBOutlet var postLabel : UILabel?
    @IBOutlet var dateLabel : UILabel!

    override func awakeFromNib() {
     postLabel?.font = UIFont(name: "Roboto-Thin", size: 14)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

【问题讨论】:

  • 我可以看到 TimelineCell 其他类型在哪里?
  • 重用单元格并在单元格中不断添加子视图以进行行方法时,可能会发生这种情况。但是,我在您发布的代码中看不到任何这种情况。还有代码吗?
  • @lcaro,我不使用的另一种类型,因为现在我尝试解决这个问题。
  • Beau,我没有任何代码可以将子视图添加到单元格。
  • @Carol,牢房里发生了什么?文本是否来自 2 个不同的单元格?还是单元格中的错位?

标签: ios xcode swift uitableview overlap


【解决方案1】:

我可以解决问题。在情节提要中,标签已取消“清除图形上下文”。我检查了,现在它解决了!感谢您的帮助!

【讨论】:

    【解决方案2】:

    过去,我的一个 UITableView 也遇到过类似的问题。有很多事情可能导致这种情况,但也许这和我发生的事情是一样的。

    我看到您正在使用自定义 tableViewCell。可能发生的情况是,当您设置单元格的文本时,它会添加一个带有该文本的标签视图。现在假设您滚动浏览 tableview 并且该单元格消失了。如果您要重用该单元格并且您没有从子视图中删除标签,或者将该标签的文本再次设置为所需的文本,您将重用带有先前标签的 tableviewcell 并添加带有新标签的新标签文本,重叠文本。

    我的建议是确保您不要继续将 UIlabels 添加为 TimelineCell 类中的子视图,除非不存在标签。如果标签存在,则编辑该标签的文本而不是单元格的文本。

    【讨论】:

    • 嗨,泰勒。我没有在单元格中添加标签。我在情节提要上有标签,只设置文本。
    【解决方案3】:

    根据apple documentation

    表格视图的数据源实现 tableView:cellForRowAtIndexPath: 应该总是重置所有内容 重复使用一个单元格。

    看来你的问题是你并不总是设置 postLabel,导致它写在其他单元格的顶部,试试这个:

    //reuse postLabel and set to blank it no value is returned by the function    
    let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell
    
                cell!.nameLabel.text = storeNew.getName()
                if newNotice.getText() != nil{
                    cell!.postLabel.text = newNotice.getText()
                } else {cell!.postLabel.text = ''}
                cell!.postLabel.numberOfLines = 0
                cell!.dateLabel.text = newNotice.getDate()
                cell!.typeImageView?.tag = indexPath.row;
                return cell!
    }
    
    //Make postLabel mandatory and set the font details in the xcode
    class TimelineCell : UITableViewCell {
    @IBOutlet var nameLabel : UILabel!
    @IBOutlet var postLabel : UILabel!
    @IBOutlet var dateLabel : UILabel!
    
    override func awakeFromNib() {
     //set this in xcode
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    

    还要确保您没有创建任何 UI 元素并将其附加到单元格,就好像您需要在回收单元格之前将其处理掉一样。

    【讨论】:

      【解决方案4】:

      您可以尝试设置:

      override func viewDidLoad() {
              super.viewDidLoad()
      
              self.tableView.rowHeight = UITableViewAutomaticDimension
              self.tableView.estimatedRowHeight = 44.0 // Set this value as a good estimation according to your cells
      }
      

      在包含您的tableView 的视图控制器中。

      确保您的 TimelineCell 中的布局约束定义了 clear 行高度约束

      另一个选项是响应:

      tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
       return 44.0 // Your height depending on the indexPath
      }
      

      始终在包含您的 tableView 的 ViewController 中,我假设是 tableViewUITableViewDelegate

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        将单元格设置为 nil 将修复一些错误。 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        var cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? ImageCellTableViewCell
            cell = nil
            if cell == nil {
                cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath) as? ImageCellTableViewCell
            }
            cell.yourcoustomtextTextLabel.text = "this is text"
            cell.yourcoustomtextImageView.image = image
            return cell
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-09
          • 2013-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多