Since the latest xcode update (to version 12) the delegate methode didSelectItemAt of a nested UIColletionView
doesn't respond.
My structure: UITableView
(vertical) - UITableViewCell
- UICollectionView
(horizontal)
The vertical tableview
consists of three sections. Two section have only one tableViewCell
each. Each of these tableViewCells
have a horizontal collectionView
.
the last section has more tableViewCells.
i'm using the tableViews didSelectRowAt method only for the third section. For section 1 and 2 i'm using the collectionViews didSelectItemAt and sending a callback via tableViewCell. but the didSelectItemAt doesn't respond to users tap.
I do not use a custom select method.
Before the update to xcode 12 (swift 5.3) everything works as expected.
Code:
private lazy var tableView: UITableView = {
let tv = UITableView(frame: CGRect.zero, style: .grouped)
tv.translatesAutoresizingMaskIntoConstraints = false
tv.delegate = self
tv.dataSource = self
tv.register(FirstHeader.self, forHeaderFooterViewReuseIdentifier: FirstHeader.reuseIdentifier)
tv.register(SecondHeader.self, forHeaderFooterViewReuseIdentifier: SecondHeader.reuseIdentifier)
tv.register(FirstTVCell.self, forCellReuseIdentifier: FirstTVCell.reuseIdentifier)
tv.register(SecondTVCell.self, forCellReuseIdentifier: SecondTVCell.reuseIdentifier)
tv.register(ThirdTVCell.self, forCellReuseIdentifier: ThirdTVCell.reuseIdentifier)
tv.alwaysBounceVertical = true
tv.isDirectionalLockEnabled = true
return tv
}()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 1:
guard let cell = tableView.dequeueReusableCell(withIdentifier: FirstTVCell.reuseIdentifier) as? FirstTVCell else { fatalError("Cant dequeue FirstTVCell") }
cell.setup(elements: firstElements, didSelect: onTap)
cell.selectionStyle = .none
return cell
case 2:...
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 3:
onCellTap?(thirdElements?[indexPath.row])
default:
return
}
}
Code of tableViewCell
with collectionView
class FirstTVCell: UITableViewCell {
static let reuseIdentifier = "FirstTVCell"
private var didSelect: ((Element?) -> Void)?
private var elements: [Element]?
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self
cv.dataSource = self
cv.register(ChipCell.self, forCellWithReuseIdentifier: ChipCell.reuseIdentifier)
return cv
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.centerYAnchor.constraint(equalTo: centerYAnchor),
collectionView.centerXAnchor.constraint(equalTo: centerXAnchor),
collectionView.widthAnchor.constraint(equalTo: widthAnchor),
collectionView.heightAnchor.constraint(equalTo: heightAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup(elements: [CoreElement]?, didSelect: ((CoreElement?) -> Void)?) {
self.didSelect = didSelect
self.elements = elements
collectionView.reloadData()
}}
extension ListChipCollectionCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
didSelect?(elements?[indexPath.row])
}}
CollectionViewCell
class ChipCell: UICollectionViewCell {
static let reuseIdentifier = "ChipCell"
private let titleLabel: UILabel = {
let l = UILabel()
l.translatesAutoresizingMaskIntoConstraints = false
l.font = .customFont(of: .semiBold, size: 14)
l.textAlignment = .center
l.numberOfLines = 1
return l
}()
func setup(element: CoreElement?) {
titleLabel.text = element?.title
addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
titleLabel.widthAnchor.constraint(equalTo: widthAnchor, constant: -20)
])
}
}