переход при нажатии на ячейку uicollectionview внутри ячейки табличного представления - PullRequest
0 голосов
/ 20 сентября 2019

У меня следующая ситуация:

ExploreViewController - это контроллер табличного представления с CategoryTableViewCell (дочерний класс UITableViewCell)

CategoryTableViewCell имеет CollectionView с UICollectionViewCells.

Когдапользователь нажимает на UICollectionViewCell, я хотел бы перейти ExploreViewController к BookListViewController на основе данных uICollectionViewCell.Каждый UICollectionViewCell обозначает отдельную категорию.

Как я могу сделать это с помощью swift?Я попытался использовать протокол делегата, но когда я вызываю executeSegue внутри функции делегата внутри ExploreViewController, я получаю ошибку.

В CategoryTableViewCell



    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("test12")
        self.delegate?.didSelectInEmbeddedCollection()
    }
}

protocol CategoryTableViewCellDelegate {
    func didSelectInEmbeddedCollection()
}

В ExploreViewController


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print("test14")
        if segue.identifier == "toBookListFeatured",
            let destination = segue.destination as? BookListViewController,
            let selectedIndexPath = self.tableViewFeaturedCats.indexPathForSelectedRow,
            let selectedCell = self.self.tableViewFeaturedCats.cellForRow(at: selectedIndexPath) as? CategoryTableViewCell
        {
            let collectionView = selectedCell.collectionView
            let indexPathC = collectionView?.indexPathsForSelectedItems?.first
            let cell = collectionView?.cellForItem(at: indexPathC!) as? CategoryCollectionViewCell
            destination.data = cell!.label.text
        } else {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            let dc = segue.destination as! BookListViewController
            dc.data = self.tableRowData["name"] as! String;
            dc.searchByCategory = true
        }
    }

}
extension ExploreViewController: CategoryTableViewCellDelegate {
    func didSelectInEmbeddedCollection() {
        performSegue(withIdentifier: "toBookListFeatured", sender: nil)
    }
}
...