【发布时间】:2018-10-03 21:14:38
【问题描述】:
我正在尝试从 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() {
}
}
【问题讨论】:
-
您确定您的 segue 标识符在情节提要中设置为“detailsSegue”吗?并且您的发件人应该是自己而不是电影而不是 FilmCell
-
它也不适用于 self 。细胞似乎根本没有对触摸做出反应。我还尝试从 ViewController 和 Cell 设置 segue。
-
情节提要中的转场标识符。设置好了吗?如果您设置为单元格或胶片,发件人仍然可以工作。这只是不好的做法
-
只需在函数中设置一个
print("hello! just didSelectItemAt !"),看看它是否会出现在您的日志中。这样我们就不必猜测您是否设置了委托或... -
@GaloTorresSevilla 是的,它设置为“detailsSegue”。
标签: ios swift uicollectionview segue uicollectionviewcell