ios - 如何选择 CollectionViewCell 中的 TableViewCell

标签 ios swift

我有一个水平滚动的 CollectionView。我有一个自定义 CollectionViewCells,它在一个包含三个单元格的 TableView 中。它看起来像这样: [![这是它的外观示例][1]][1]

这是主要的 ViewController 我有 CollectionView

extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.viewModel.numberOfItems
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ItemCell.reuseIdentifier, for: indexPath) as! ItemCell

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedItem = self.viewModel.items[indexPath.item]
    }
}

这是包含表格 View 的ItemCell代码

class ItemCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        self.tableView.allowsMultipleSelection = true
        self.tableView.register(UINib(nibName: "DailySelectionViewCell", bundle: nil), forCellReuseIdentifier: DailySelectionViewCell.reuseIdentifier)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: DailySelectionViewCell.reuseIdentifier, for: indexPath) as! DailySelectionViewCell

        switch indexPath.section {
        case 0:
            cell.descriptionLabel.text = "Item1"
        case 1:
            cell.descriptionLabel.text = "Item2"
        default:
            cell.descriptionLabel.text = "Item3"
        }

        return cell
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect.zero)
        view.backgroundColor = UIColor.clear
        return view
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

我想单击 CollectionView 单元格内的 TableView 单元格。当我想点击 TableViewCell 时,触摸仅在 CollectionViewCell 上注册,我无法点击 CollectionViewCell< 内的任何 tableview 单元格.

最佳答案

默认情况下 didSelectRowAt 必须有效。 假设 DailySelectionViewCell 可能包含一些与用户交互的 View ,即 UIControl 的子类:UIButtonUIDatePickerUISegmentedControl 等等。在这种情况下,didSelectRowAt 不起作用,这是正确的行为。

关于ios - 如何选择 CollectionViewCell 中的 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57714496/

相关文章:

ios - Android 类似 iOS 中的权限

ios - Xcode 搜索和销毁

swift - 从 Fabric 迁移到 Firebase 时未提供 Google App ID 或 Google 服务文件

ios - swift: nsurlsession 下载文件

iphone - 从 View 中显示新 View 然后在 iphone 中返回到它的正确方法是什么

ios - 检查 URL 在 Swift 4 中是否有效

swift - 为什么我的 SemVer NSRegularExpression 不能运行?

swift - 删除 PencilKit 中的内容

ios - 关闭模态视图时在 VC 中触发函数

arrays - 如何使用 ExSwift Array.toDictionary?