动态原型UITableViewCell中的可重复内容

时间:2016-02-18 09:38:27

标签: ios swift uitableview nslayoutconstraint

我有一个问题,我有原型单元格,上面有6个标签,之后我有随机数量的视图,左侧有图片,右侧有文字,因为它在Picture 1上。

我尝试制作一个xib文件,用于表示带有图片和标签的视图及其约束Picture 2

然后我尝试制作该视图并将其添加约束到单元格。看起来约束是完全错误的。这就是我尝试设置约束的方法:

    for (index, entity) in linkedEntities.enumerate() {
            let linkedEntityView: LinkedEntityInExtendedSearchNotes = UIView.fromNib()
            linkedEntityView.entity = entity
            addSubview(linkedEntityView)
            linkedEntityView.translatesAutoresizingMaskIntoConstraints = false
            print(self.dynamicType)
            if index == 0 {
                let verticalSpace = NSLayoutConstraint(item: linkedEntityView, attribute: .Top, relatedBy: .Equal, toItem: noteTextLabel, attribute: .Bottom, multiplier: 1, constant: 8)
                let leading = NSLayoutConstraint(item: linkedEntityView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .LeadingMargin, multiplier: 1, constant: 8)
                let trailing = NSLayoutConstraint(item: linkedEntityView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 8)
                NSLayoutConstraint.activateConstraints([verticalSpace, leading, trailing])
            } else if index > 0 && index < linkedEntities.count - 1 {
                let verticalSpace = NSLayoutConstraint(item: linkedEntityView, attribute: .Top, relatedBy: .Equal, toItem: previousView, attribute: .Bottom, multiplier: 1, constant: 8)
                let leading = NSLayoutConstraint(item: linkedEntityView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 8)
                let trailing = NSLayoutConstraint(item: linkedEntityView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 8)
                NSLayoutConstraint.activateConstraints([verticalSpace, leading, trailing])
            } else {
                let verticalSpace = NSLayoutConstraint(item: linkedEntityView, attribute: .Top, relatedBy: .Equal, toItem: previousView, attribute: .Bottom, multiplier: 1, constant: 8)
                let leading = NSLayoutConstraint(item: linkedEntityView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .LeadingMargin, multiplier: 1, constant: 8)
                let trailing = NSLayoutConstraint(item: linkedEntityView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 8 )
                let bottomSpace = NSLayoutConstraint(item: linkedEntityView, attribute: .BottomMargin, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 8)
                NSLayoutConstraint.activateConstraints([verticalSpace, leading, trailing, bottomSpace])
            }

            previousView = linkedEntityView
        }

noteTextLabel是重复内容之前的最后一个标签。哪里我的约束错了?有没有其他(更好的)方法将可重复内容放入原型单元格?

感谢。

0 个答案:

没有答案