Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIViews在swift中将多个视图动态添加到tableViewCell时重叠_Ios_Swift_Uitableview - Fatal编程技术网

Ios UIViews在swift中将多个视图动态添加到tableViewCell时重叠

Ios UIViews在swift中将多个视图动态添加到tableViewCell时重叠,ios,swift,uitableview,Ios,Swift,Uitableview,我想在表格单元格中添加一些n视图(假设每个视图都是一行)。根据designerExpertiseListcount,我为每行创建了一个视图,并添加了一个图像视图和一个标签 但当我滚动时,单元格的数据不正确。如果我长按一个单元格,我可以看到一个不同的视图与现在可见的视图重叠。请检查附加的屏幕截图 第一次加载视图时: 向下滚动后,再次向上滚动并长按: 当我向上滚动时,第一次对少数单元格正确的数据,即使是这些数据也被弄乱了。在第一次渲染时,我甚至只尝试添加一次这些动态视图。 有全局声明: let ro

我想在表格单元格中添加一些
n
视图(假设每个视图都是一行)。根据
designerExpertiseList
count,我为每行创建了一个视图,并添加了一个图像视图和一个标签

但当我滚动时,单元格的数据不正确。如果我长按一个单元格,我可以看到一个不同的视图与现在可见的视图重叠。请检查附加的屏幕截图
第一次加载视图时:
向下滚动后,再次向上滚动并长按:

当我向上滚动时,第一次对少数单元格正确的数据,即使是这些数据也被弄乱了。在第一次渲染时,我甚至只尝试添加一次这些
动态视图。


有全局声明:

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]()

表视图中的代码
cellforrowatinexpath
方法:

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

我认为只有当self.firstRender[indexPath.section]
时才渲染单元格是不正确的,因为当用户滚动表视图时,屏幕外的单元格将被重用以显示其他不同的indexPath。
另外,您可以创建约束,但不使用它们

您可以使用UIStackView为您处理约束。你会:

  • 大大减少类中的样板约束代码
  • 简化添加子视图
  • 允许自适应布局 将垂直UIStackViewFor添加到UITableViewCell中。对于更复杂的布局,可以在垂直堆栈视图中嵌入更多的UIStackView,也可以混合使用AutoLayout和UIStackView

    我很快尝试重新创建您的布局,并添加了一个视图。此实现对某些固定组件(如配置文件图片)使用AutoLayout,并使用堆栈视图处理以编程方式添加的UILabel


    我希望这有帮助

    似乎,使用.addSubView()以编程方式将视图添加到单元格中不适合使用可重用单元格。这种情况发生的原因是,当单元格离开视图时会重新使用,但当再次可见时,子视图会再次添加

    我使用的解决方案/解决方法是从故事板添加另一个占位符视图。在不需要时,我使用.removeFromSuperview()从superview中删除了该视图

    我知道这个方法不好 例如:假设我在一个单元格中放置了10个视图(假设10个视图是我需要的最大视图),但根据我从服务器获得的数据,其中许多视图可能不需要。因此,我将从视图中删除其中的大部分


    我仍在为这个问题寻找更好的/实际的解决方案。

    您是否添加了限制条件
    view.addConstraint
    查看我正在使用的代码。active=true,将激活约束。不需要单独添加约束。至少这是我读到的嗨,我不想使用堆栈视图,因为它只适用于iOS 9+。我的目标是印度观众,他们中的一些用户仍然使用iOS 7和iOS 8。堆栈视图看起来很有前途,但我决定不使用它,原因与上面相同,我只为动态添加的元素添加了这个条件。将呈现其他UI元素。我正在使用.active=true创建约束并直接激活。我假设将呈现一个单元格,例如位于indexPath(0)的单元格,将调用委托方法
    cellforrowatinexpath
    ,您的代码将正确动态添加子视图,因为
    self.firstRender[0]
    为true。然后用户滚动表格视图,单元格(0,0)变得不可见,所以单元格(0,0)将被另一个单元格重用。当单元格(0,0)再次可见时,
    cellforrowatinexpath
    中的代码将不会执行,因为
    self.firstRender[0]
    为false,则不会添加任何子视图,这是您想要的吗?我添加了firstRender检查,因为我认为它会阻止将动态视图多次添加到同一单元格中。但现在我想我会采取不同的方法。我要三排。如果不需要,我将只做删除子view@kishorer747建议您做一个实验,在多个屏幕上添加一些行。另外,我是一名Objective-C编程人员,对Swift知之甚少。在Object-C中,您应该调用
    [self.view addConstraint:constraint]以应用约束。是否不需要在Swift中调用
    self.view.addConstraint(constraint)
    ?我在苹果的文档中搜索,似乎你也应该这样做