当我打印我在 MyCell 类中声明的 IBOutlets 时,它们显示为零。

ViewController 中的方法

public override func viewDidLoad() {
    super.viewDidLoad()
    self.seriesCollectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
    cell.setup()

    return cell
}

MyCell 类
class MTSeriesViewCell: UICollectionViewCell {
@IBOutlet weak var cellView: UIView!

@IBOutlet weak var cellImageView: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
}

public func setup() {
    print(cellView)
}

}

有没有其他方法可以在 ViewController 的 viewDidLoad 中初始化/注册 MyCell 类?

最佳答案

代替:
self.seriesCollectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")
用:

self.seriesCollectionView.register(UINib(nibName: "MyCellXibName", bundle: nil), forCellWithReuseIdentifier: "MyCell")

关于ios - UICollectionView 中的 IBOutlets 为零 - Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43655113/

10-13 09:48