【发布时间】:2018-06-15 00:18:07
【问题描述】:
我有一个包含多个单元格的 collectionView。每个单元格都属于此类:
class OuterCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var stackView: UIStackView!
override init(frame: CGRect) {
super.init(frame: frame)
print("initting")
for i in 1...30 {
let view = UIView()
view.backgroundColor = UIColor.red
view.tag = i
self.stackView.addSubview(view)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我正在创建每个单元格,我希望像这样调用它的初始化程序:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "outerCell", for: indexPath) as! OuterCollectionViewCell
cell.backgroundColor = UIColor.gray
return cell
}
我的意图是向这个水平 stackView 添加 30 个子视图,以便显示 30 个条纹。我想在 init 中以编程方式添加这些子视图,以避免在情节提要中手动执行。但是现在没有调用 init 并且没有添加视图。知道为什么会这样以及如何做到这一点吗?
【问题讨论】:
标签: ios swift uicollectionview uistackview