С моего основного контроллера представления, как перехватить и отправить на следующий контроллер представления элемент, выбранный, нажав UICollectionViewCell представления коллекции внутри tableViewCell?
мой главный контроллер:
class ViewController: UIViewController {
@IBOutlet weak var myTable: UITableView!
private let kCellId = "myTableCell"
var paymentMethods: [Item] = []
override func viewDidLoad() {
super.viewDidLoad()
myTable.delegate = self
myTable.dataSource = self
paymentMethods.append(Item(name: "item 1", image: nil))
paymentMethods.append(Item(name: "item 2", image: nil))
paymentMethods.append(Item(name: "item 3", image: nil))
paymentMethods.append(Item(name: "item 4", image: nil))
paymentMethods.append(Item(name: "item 5", image: nil))
paymentMethods.append(Item(name: "item 6", image: nil))
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = myTable.dequeueReusableCell(withIdentifier: self.kCellId, for: indexPath) as? MyTableCell {
cell.backgroundColor = .purple
cell.myCollectionView.backgroundColor = .green
cell.arrayInCell = self.paymentMethods
return cell
}
return UITableViewCell()
}
}
моя клетка:
import UIKit
class MyTableCell: UITableViewCell {
@IBOutlet weak var myCollectionView: UICollectionView!
let kCellId = "myCollViewCellId"
var arrayInCell: [Item] = []
override func awakeFromNib() {
super.awakeFromNib()
myCollectionView.delegate = self
myCollectionView.dataSource = self
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
print("count: ", arrayInCell.count) //this is zero but array is used
if arrayInCell.count > 5 {
layout.itemSize = CGSize(width: self.bounds.width/5.5, height: self.bounds.height)
} else {
layout.itemSize = CGSize(width: self.bounds.width/5, height: self.bounds.height)
}
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1
myCollectionView.collectionViewLayout = layout
}
}
extension MyTableCell: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(arrayInCell[indexPath.row].name ?? "error")
}
}
extension MyTableCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayInCell.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: self.kCellId, for: indexPath) as? MyCollectionCell {
cell.backgroundColor = .systemTeal
cell.collectionCellLabel.text = arrayInCell[indexPath.row].name
cell.collectionCellLabel.numberOfLines = 0
return cell
}
return UICollectionViewCell()
}
}