OStack程序员社区-中国程序员成长平台

标题: ios - 快速将多个 View 动态添加到 tableViewCell 时 UIViews 重叠 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:44
标题: ios - 快速将多个 View 动态添加到 tableViewCell 时 UIViews 重叠

我想在表格单元格中添加一些 n View (假设每个 View 都是一行)。根据 designerExpertiseList 计数,我为每一行创建了一个 View ,并添加了 1 个 ImageView 和 1 个标签。

但是当我滚动时,单元格的数据不正确。如果我长按一个单元格,我可以看到一个不同的 View 与现在可见的 View 重叠。请检查随附的屏幕截图

第一次加载 View 时:http://i.stack.imgur.com/M8itL.png
我向下滚动后,再次向上滚动并长按:http://i.stack.imgur.com/AuTG0.png

当我向上滚动时,第一次正确的几个单元格的数据,即使是搞砸了。我什至尝试在第一次渲染时只添加一次这些动态 View

有全局声明:

let rowHeight:CGFloat = 20.0
let imageWidth:CGFloat = 15.0
let imageHeight:CGFloat = 15.0
let labelX:CGFloat = 30.0
let labelHeight:CGFloat = 20.0
var firstRender:[Bool] = [Bool]()


tableView中的代码cellForRowAtIndexPath方法:

self.designer = AppController.getElementFromDesignersList(indexPath.section)

        cell.designerName.text = designer.getFirstName() + " " + designer.getLastName()
        cell.location.text = designer.getAddress()

        // Add more ROWS of expertise if exist! Getting only 1st expertise now, if it exists
        let expertiseList = self.designer.getExpertiseList()

        if self.firstRender[indexPath.section] {
            var i:Int = 0
            for e in expertiseList {
                let v = UIView(frame: CGRectMake(0, CGFloat(i)*rowHeight, cell.frame.width, rowHeight))
                v.backgroundColor = UIColor.yellowColor()
                let im = UIImageView(frame: CGRectMake(0, CGFloat(i)*imageHeight, imageWidth, imageHeight ))
                //print("expertise image path: ", e.getImagePath())
                im.af_setImageWithURL(
                    NSURL(string: e.getImagePath())!,
                    placeholderImage: UIImage(named: "default_galary_demo2")!
                )

                im.translatesAutoresizingMaskIntoConstraints = false
                v.addSubview(im)

                // Adding constraints
                NSLayoutConstraint(item: im, attribute: .CenterY, relatedBy: .Equal, toItem: v, attribute: .CenterY, multiplier: 1, constant: 0).active = true
                NSLayoutConstraint(item: im, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: v, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 0.0).active = true
                im.widthAnchor.constraintEqualToAnchor(nil, constant: imageWidth).active = true
                im.heightAnchor.constraintEqualToAnchor(nil, constant: imageHeight).active = true

                // cell.frame.width - im.frame.width - 50
                let label = UILabel(frame: CGRectMake(0, CGFloat(i)*labelHeight, UIScreen.mainScreen().bounds.width - imageWidth, labelHeight))
                label.font = UIFont(name: "OpenSans", size: 12)
                print("expertise dump:  ", dump(e.getExpertiseValuesList()))
                //print("expertise str: ", e.getExpertiseValuesList().map({"\($0.getName())"}).joinWithSeparator(","))
                label.text = e.getExpertiseValuesList().map({"\($0.getName())"}).joinWithSeparator(",")

                //label.translatesAutoresizingMaskIntoConstraints = false
                v.addSubview(label)


                NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: v, attribute: .CenterY, multiplier: 1, constant: 0).active = true

                NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: im, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 10.0).active = true

                cell.designerExpertiseView.addSubview(v)
                i += 1
            }
            self.firstRender[indexPath.section] = false



Best Answer-推荐答案


我认为仅在 if self.firstRender[indexPath.section] 时渲染单元格是不正确的,因为当用户滚动表格 View 时,屏幕外的单元格将被重用以显示其他不同索引路径。 另一件事,您创建约束,但不使用它们

关于ios - 快速将多个 View 动态添加到 tableViewCell 时 UIViews 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836510/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4