UICollectionView в UIControl - делегат и источник данных - PullRequest
1 голос
/ 13 января 2020

Я добавил UICollectionView к UIControl (который вызывается в заголовке раздела UITableViewController), и появляется UICollectionView (цвет фона показывает), но источник данных и делегат, кажется, игнорируется - ячейки не отображаются, и оператор print в моей функции dequeueReusableCell не вызывается (но один из методов UIControl init есть. Можете ли вы сказать мне, что мне здесь не хватает?

Мой UIControl:

// Stripped down code
final class My_Control: UIControl {
  override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(myCV)
  }

  private let myCV: Custom_CV = {
    let cv: Custom_CV = Custom_CV()
    return cv
  }()
}

Просмотр моей коллекции:

final class Custom_CV: UIControl, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(collectionView)        
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
  }

  func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
    cell.backgroundColor = .red
    print("Building cell") // Not being called
    return cell
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 72.0, height: 72.0)
  }

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //
  }

  private let cellIdentifier: String = "cellIdentifier"

  private let collectionView: UICollectionView = {
    let layout: UICollectionViewLayout = UICollectionViewLayout()
    let collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = .white
    return collectionView
  }()
}

Я думаю, что упускаю что-то очевидное, но я Я не вижу этого. Я ожидаю увидеть 10 красных квадратов и 10 операторов печати, но я ничего не получу. Но, как я уже сказал, он определенно вызывается, потому что вызывается оператор печати в инициализации My_CV. и я вижу фон .white для просмотра коллекции в My_Control.

...