我正在尝试从UICollectionViewCell筛选到新的ViewController。我已经从一个ViewController到另一个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
FilmCell类:
class FilmCell: UICollectionViewCell {
@IBOutlet var posterImage: UIImageView!
@IBOutlet var titleLabel: UILabel!
override func awakeFromNib() {
}
}
答案 0 :(得分:0)
尝试一下:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegue(withIdentifier: "detailsSegue", sender: nil)
}
答案 1 :(得分:0)
我有一个UITapGesture,用于退出连接到收藏夹视图的键盘。我100%可以肯定我已经尝试删除该手势及其代码。显然,我没有完全删除它,因为我返回并再次尝试,这解决了问题。集合视图内的“轻按手势”位于单元格上方,导致触摸时无法选择它们。
解决方案是,UICollectionView中没有UITapGesture。