在以编程方式添加的 UICollectionViewCell 中看不到 UILabel
Cannot see UILabel in a UICollectionViewCell added programmatically
当用户在我的 CollectionView 上启动 PanGesture 以重新排序单元格时,我以编程方式创建了一个 UICollectionViewCell(我不使用 Apple API,因为我必须对行为进行个性化设置)。
我从具有 IBOutlet 的自定义 class WordCollectionViewCell
创建了一个 Cell。这是 class:
的代码
class WordCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var wordLabel: UILabel!
}
要创建单元格并将其添加到 CollectionView,我使用以下代码:
self.movingCell = {
let mC = WordCollectionViewCell(frame: selectedCell.frame)
// I HAVE TO DO THIS WITH A VARIABLE BECAUSE IF I ASSIGN UILabel() DIRECTLY TO mC.wordlabel, THE APP CRASH BECAUSE wordLabel IS NIL
let label = UILabel()
mC.wordLabel = label
mC.wordLabel.text = selectedCell.wordLabel.text
mC.addSubview(mC.wordLabel)
mC.backgroundColor = selectedCell.backgroundColor
mC.layer.cornerRadius = selectedCell.layer.cornerRadius
mC.tag = selectedIndexPath.row
return mC
}()
self.collectionView.addSubview(self.movingCell)
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
Cell 将跟随 PanGesture 位置。
这段代码有两个问题:
- 在我的设备上
movingCell
测试中我什么也没看到;
- 为什么我不能直接将
UILabel()
设置为wordLabel
?
尝试添加 label.translatesAutoresizingMaskIntoConstraints = false
当用户在我的 CollectionView 上启动 PanGesture 以重新排序单元格时,我以编程方式创建了一个 UICollectionViewCell(我不使用 Apple API,因为我必须对行为进行个性化设置)。
我从具有 IBOutlet 的自定义 class WordCollectionViewCell
创建了一个 Cell。这是 class:
class WordCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var wordLabel: UILabel!
}
要创建单元格并将其添加到 CollectionView,我使用以下代码:
self.movingCell = {
let mC = WordCollectionViewCell(frame: selectedCell.frame)
// I HAVE TO DO THIS WITH A VARIABLE BECAUSE IF I ASSIGN UILabel() DIRECTLY TO mC.wordlabel, THE APP CRASH BECAUSE wordLabel IS NIL
let label = UILabel()
mC.wordLabel = label
mC.wordLabel.text = selectedCell.wordLabel.text
mC.addSubview(mC.wordLabel)
mC.backgroundColor = selectedCell.backgroundColor
mC.layer.cornerRadius = selectedCell.layer.cornerRadius
mC.tag = selectedIndexPath.row
return mC
}()
self.collectionView.addSubview(self.movingCell)
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
self.collectionView.addConstraint(NSLayoutConstraint(item: movingCell.wordLabel, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: movingCell, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
Cell 将跟随 PanGesture 位置。
这段代码有两个问题:
- 在我的设备上
movingCell
测试中我什么也没看到; - 为什么我不能直接将
UILabel()
设置为wordLabel
?
尝试添加 label.translatesAutoresizingMaskIntoConstraints = false