UICollectionViewCell Segue to new ViewController



我正在尝试从UICollectionViewCell转移到新的ViewController。我已经创建了一个从一个视图控制器到另一个视图控制器的续集,并在选择单元格时创建了下面的代码。

class FilmsViewController: UIViewController, UICollectionViewDelegate, 
UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, 
UISearchBarDelegate {
var films: [NSDictionary]?
var filteredFilms: [NSDictionary]?
let searchBar = UISearchBar()
//let searchController = UISearchController(searchResultsController: nil)
@IBOutlet var filmsTable: UICollectionView!
@IBOutlet var navLogo: UINavigationItem!
@IBOutlet var search: UIBarButtonItem!
@IBOutlet var back: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()

self.filmsTable.dataSource = self
self.filmsTable.delegate = self
loadFilms()
}
func collectionView(_ _collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return filteredFilms?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = filmsTable.dequeueReusableCell(withReuseIdentifier: "filmCell", for: indexPath) as! FilmCell
let film = filteredFilms![indexPath.row]
let title = film["title"] as! String
cell.titleLabel.text = title

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: NSIndexPath) {
let film = filteredFilms![indexPath.row]
performSegue(withIdentifier: "detailsSegue", sender: film)
}

问题是当我选择单元格时没有任何反应。没有错误,只是对单击任何单元格都没有响应。我相信问题是单元格没有响应被点击,但我不确定为什么。

编辑:我也尝试过:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: NSIndexPath) {
performSegue(withIdentifier: "detailsSegue", sender: FilmCell)
}

FilmCell是UICollectionViewCell

电影细胞类:

class FilmCell: UICollectionViewCell {

@IBOutlet var posterImage: UIImageView!
@IBOutlet var titleLabel: UILabel!
override func awakeFromNib() {
}
}

试试这个:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegue(withIdentifier: "detailsSegue", sender: nil)
}

我有一个用于重新签名连接到集合视图的键盘的 UITapGesture。我 100% 以上确定我已经尝试删除手势及其代码。显然我没有完全删除它,因为我回去再试一次,这解决了问题。点按 收藏夹视图中的手势位于单元格上方,导致在触摸时无法选择它们。

解决方案是,不要在你的UICollectionView中使用UITapGesture。

最新更新