【发布时间】:2017-08-02 18:48:02
【问题描述】:
我有一个UITableView,其中一个单元格包含UICollectionView。
我已将UITableViewCell 设置为UICollectionView 的委托和数据源,在这里我已经实现了我需要的所有方法。
问题在于,就像我的 UICollectionView 不响应触摸事件:滚动或触摸也不起作用。
所以,collectionView(_:didSelectItemAt:indexPath) 永远不会被调用。
UserInteractionIsEnabled 设置为 TRUE 以:
1. TableView
2. TableViewCell
3. ContentView
4. CollectionView
5. CollectionViewCell
我真的不知道我的代码出了什么问题。
这是 TableViewCell 的代码
import UIKit
class InsertSecondCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
let COUNT = 12
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.isPagingEnabled = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.isScrollEnabled = true
collectionView.isUserInteractionEnabled = true
collectionView.reloadData()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return COUNT
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell",
for: indexPath) as! CategoryCollectionCell
cell.allergenImageView.image = UIImage(named: "IMAGE")
cell.titleLabel.text = "TITLE"
cell.bottomViewm.isHidden = false
cell.backgroundColor = UIColor.white
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
cell.layer.cornerRadius = 2.5
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("hi")
}
}
【问题讨论】:
-
数据源方法工作正常。
-
我使用了相同的方法,在另一个视图控制器的 tableviewcell 中拥有数据源和委托,一切正常。
-
你的单元格叫做 InsertDishSecondCell 还是 secondSectionCell?
-
第一个是UITableViewCell子类的名字,第二个是我给cell的id。
标签: ios iphone uitableview uicollectionview