首页 > 解决方案 > 具有动态创建高度的单元格重叠

问题描述

嗨,我对 IOS 开发人员较新,对 Web 开发人员更熟悉。我遇到了一个问题,我的单元格最初按照我的预期呈现,但是在滚动到底部并滚动回屏幕顶部后,我注意到单元格开始相互重叠。在阅读了其他堆栈溢出帖子后,我开始认为这与我如何创建单元格有关。我试图在一个带有 for 循环的单元格中创建多个标签,类似于有人使用 PHP for web dev 的方式,我想知道这是否是正确的方法/这是一个可能的原因。

                       for i in 0...bars.count-1 {
                            var barLabelView:UIView = UIView()
                            var barLabel:UILabel = UILabel()
                            barLabelView.frame = CGRect(x: 20, y: CGFloat(20 + (70 * i)), width: barView.frame.width-40, height: 50)
                            barLabelView.backgroundColor = UIColor(displayP3Red: 27/255.0, green: 99/255.0, blue: 222/255.0, alpha: 1)
                            barLabelView.layer.cornerRadius = 10
                            barLabel.frame = CGRect(x: 0, y: 0, width: barLabelView.frame.width-10, height: 50)
                            barLabel.text = bars[i]
                            barLabel.textAlignment = .center
                            barLabel.numberOfLines = 0
                            barLabel.font = UIFont(name: "Chalkboard SE", size: 15)
                            barView.addSubview(barLabelView)
                            barLabelView.addSubview(barLabel)

                        }

还有我如何设置单元格的高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let crawlNameText = self.posts[indexPath.row].text
        let size = CGSize(width: view.frame.width - 40, height: 1000)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let estimatedCrawlNameFrame = NSString(string: crawlNameText).boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], context: nil)


        let bars = posts[indexPath.row].bars.components(separatedBy: ", ")
        let buds = posts[indexPath.row].buds.components(separatedBy: ", ")

        if (self.posts[indexPath.row].type == "crawl") {

            var cellHeightP1 = CGFloat(estimatedCrawlNameFrame.height + 110 + 20 + 20 + 20 + 40 + 60)
            var cellHeightP2:CGFloat = CGFloat( 80 * bars.count)
            var cellHeightP3:CGFloat = CGFloat( 80 * buds.count)
            var cellHeight = cellHeightP1 + cellHeightP2 + cellHeightP3

            return cellHeight
        } else {
             return estimatedCrawlNameFrame.width + 110 + 20
        }
    }

这是我的 cellForRowAt 函数

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell = nfTV.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! newsFeedCell

             // Im guessing I would set the cell to nill here to clear the buttons I made

            cell.setPost(post: posts[indexPath.row])

            if (cell.profilePic != nil){
                cell.profilePic.layer.cornerRadius = cell.profilePic.bounds.height / 2
                cell.profilePic.clipsToBounds = true
            }

            cell.backgroundColor = UIColor.lightGray

            print("made it here")

            print(indexPath.row)

            cell.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)



            return cell
}

我读到我可能必须在重用它之前清除我的单元格,但我不确定如何将单元格设置为零。我也想知道使用集合视图是否会更好,但我不知道它和表格视图之间的区别。任何帮助或建议表示赞赏,谢谢。

标签: iosswiftuitableviewuicollectionview

解决方案


我回答了我自己的问题,但我想我会留下这个,以防将来对任何人有帮助

我在 cellForRowAt 函数的注释中添加了以下代码行以删除按钮

 for v in cell.barView.subviews{
       v.removeFromSuperview()
    }

    for v in cell.budsView.subviews{
       v.removeFromSuperview()
    }

该问题是由于某些单元格的按钮比其他单元格多,因此按钮较少的单元格呈现的按钮数量超过了必要的数量,使其看起来像是重叠的,但实际上它只是回收的按钮。


推荐阅读