【问题标题】:Delegate Methods of UICollectionView insider UITableView not called未调用 UICollectionView 内部 UITableView 的委托方法
【发布时间】: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


【解决方案1】:

对于滚动,您是否在 IB 中检查了弹跳和垂直/水平弹跳?

至于选择,如果你长按单元格,它最终会打印你的消息吗?您可以添加tapGestureRecognizer.cancelsTouchesInView = false 以帮助手势识别器更快地超越视图层次结构。见here

【讨论】:

  • 我已经设置了反弹,但它根本不滚动。而且,虽然我长按,它不打印任何东西。我真的很难理解为什么会这样。
  • 也许尝试删除您在 IB 中连接的代理和数据源插座,并将您在 UITableViewCell 子类中的delegate/datasource = self 部分保留?
  • 不幸的是它不起作用。我真的不知道该怎么办。
  • 您能否更新您的问题以包含 UITableView cellForRowAt 方法的代码?
  • 我通过从情节提要中的另一个视图控制器复制单元格来解决它。代码没变,我觉得是约束的问题。
猜你喜欢
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
相关资源
最近更新 更多