CombineCocoa icon indicating copy to clipboard operation
CombineCocoa copied to clipboard

tableView(_:viewForHeaderInSection:) delegate method cannot be used

Open shinichy opened this issue 3 years ago • 2 comments

Describe the bug tableView(_:viewForHeaderInSection:) delegate method cannot be used when using tableView.didEndScrollingAnimationPublisher.

To Reproduce

class MyViewController: UIViewController {
    @IBOutlet private weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.didEndScrollingAnimationPublisher
            .sink { print("didEndScrollingAnimation") }
    }
}

extension MyViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        // return my header view
    }
}

MyViewController is configured as a delegate of tableView in a storyboard.

Expected behavior tableView(_:viewForHeaderInSection:) is called

Device:

  • Device: iPhone 14
  • OS: iOS 16
  • 0.4.1

Additional context RxCocoa supports this by calling tableView.rx.setDelegate(viewController).

shinichy avatar Nov 02 '22 22:11 shinichy

代理 和 pulisher 只能使用其中的一个. 使用了 publisher 之前scroller.delegate 被重新赋值

BestiOSDev avatar Nov 16 '22 08:11 BestiOSDev

We had a similar trouble with needing to use indexPathForPreferredFocusedView. Our solution:

class CollectionView: UICollectionView {
  
  // Subclass of `UICollectionView` provides ability to provide a index path for preferred focus closure.
  // Since we're using `CombineCocoa` we lose the ability to use `UICollectionViewDelegate.indexPathForPreferredFocusedView`
  
  var indexPathForPreferredFocused: (() -> IndexPath?)?
  
  override var preferredFocusEnvironments: [UIFocusEnvironment] {
    let indexPath = indexPathForPreferredFocused?()
    let cell = indexPath.map { self.cellForItem(at: $0) }
    return cell?.map { [$0] } ?? super.preferredFocusEnvironments
  }
}

markst avatar Sep 13 '23 01:09 markst