![](/img/trans.png)
[英]UICollectionViewCell Custom Segue ViewController Connection error
[英]UICollectionViewCell Segue to new ViewController
我正在嘗試從 UICollectionViewCell 轉到新的 ViewController。 我已經創建了一個從一個 ViewController 到另一個的 segue,並在我選擇單元格時創建了下面的代碼。
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() {
}
}
嘗試這個 :
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegue(withIdentifier: "detailsSegue", sender: nil)
}
我有一個 UITapGesture 用於退出連接到集合視圖的鍵盤。 我 100% 以上確定我已經嘗試刪除手勢和它的代碼。 顯然我沒有完全刪除它,因為我回去再試一次,這解決了問題。 點擊集合視圖內的手勢位於單元格上,導致它們無法在觸摸時被選中。
解決方案是,在您的 UICollectionView 中不要有 UITapGesture。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.