У меня проблема с представлением моей коллекции. Я пытаюсь выбрать indexPath элемента, чтобы отобразить дополнительную информацию о нем после нажатия, но я не могу его получить и понять, почему.
Я попытался разрешить выбор вида коллекции, но ничего не вышло. Я также попытался добавить жест касания к ячейке, но не знаю, как получить оттуда indexPath. Я добавляю тот же код на другой V C, где представление коллекции отображается на весь экран, и он работает (здесь представление коллекции занимает половину экрана).
Вот (обновленный) код для коллекции Вид:
class SearchRunnerVC: UIViewController {
//MARK: - Objects
var lastNameTextField = RSTextField(placeholder: "Nom du coureur", returnKeyType: .next,
keyboardType: .default)
var firstNameTextField = RSTextField(placeholder: "Prénom", returnKeyType: .next,
keyboardType: .default)
var numberTextField = RSTextField(placeholder: "Numéro de dossard", returnKeyType: .next,
keyboardType: .numberPad)
var raceTextField = RSTextField(placeholder: "Course", returnKeyType: .done,
keyboardType: .default)
var searchButton = RSButton(backgroundColor: .systemPink, title: "Rechercher")
var collectionView : UICollectionView! = nil
var emptyLabel = UILabel()
let hud = JGProgressHUD(style: .dark)
lazy var textFields = [lastNameTextField, firstNameTextField, numberTextField, raceTextField]
//MARK: - Properties
let padding = CGFloat(20)
let runnersCollection = Firestore.firestore().collection("Runner")
var runners = [Runner]()
//MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
title = "Rechercher"
}
}
extension SearchRunnerVC {
fileprivate func setupUI() {
configureSuperView()
configureTextFields()
configureCollectionView()
configureButton()
configureConstraints()
}
fileprivate func configureSuperView() {
view.backgroundColor = .systemBackground
let viewTap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard(_:)))
view.addGestureRecognizer(viewTap)
}
fileprivate func configureConstraints() {
for textField in textFields {
view.addSubview(textField)
textField.leftToSuperview(view.leftAnchor, offset: padding)
textField.rightToSuperview(view.rightAnchor, offset: -padding)
textField.height(50)
textField.delegate = self
}
firstNameTextField.topToSuperview(view.safeAreaLayoutGuide.topAnchor, offset: padding, usingSafeArea: true)
lastNameTextField.topToBottom(of: firstNameTextField, offset: padding)
numberTextField.topToBottom(of: lastNameTextField, offset: padding)
raceTextField.topToBottom(of: numberTextField, offset: padding)
searchButton.topToBottom(of: raceTextField, offset: padding)
searchButton.leftToSuperview(view.leftAnchor, offset: padding)
searchButton.rightToSuperview(view.rightAnchor, offset: -padding)
searchButton.height(50)
collectionView.topToBottom(of: searchButton, offset: padding)
collectionView.edgesToSuperview(excluding: .top)
}
fileprivate func configureTextFields() {
firstNameTextField.tag = 0
lastNameTextField.tag = 1
numberTextField.tag = 2
raceTextField.tag = 3
}
fileprivate func configureButton() {
view.addSubview(searchButton)
searchButton.addTarget(self, action: #selector(searchRunners), for: .touchUpInside)
}
fileprivate func createLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .estimated(100))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
let spacing = CGFloat(10)
group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = spacing
let padding = CGFloat(10)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: padding, bottom: 0, trailing: padding)
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(80))
let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
section.boundarySupplementaryItems = [header]
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
fileprivate func configureCollectionView() {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleHeight]
collectionView.backgroundColor = .systemBackground
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(RunnerCell.self, forCellWithReuseIdentifier: RunnerCell.reuseID)
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Header.reuseID)
view.addSubview(collectionView)
}
}
extension SearchRunnerVC : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return runners.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RunnerCell.reuseID, for: indexPath) as? RunnerCell else { fatalError("Unable to dequeue runner cell")}
cell.configure(runner: runners[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Header.reuseID, for: indexPath) as? Header else { fatalError("Unable to dequeue header")}
header.title.text = "Résultats"
header.seeButton.addTarget(self, action: #selector(seeAllTapped), for: .touchUpInside)
header.separator.alpha = 0
header.backgroundColor = collectionView.backgroundColor
return header
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
}
}