CombineCocoa
CombineCocoa copied to clipboard
tableView(_:viewForHeaderInSection:) delegate method cannot be used
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).
代理 和 pulisher 只能使用其中的一个. 使用了 publisher 之前scroller.delegate 被重新赋值
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
}
}