如何知道在哪个部分中点击了单元格

时间:2019-05-27 16:53:00

标签: swift

我在TableViewCell的内部有CollectionView,当我在CollectionView中选择一个随机单元格时,如何知道它在哪个TableView的部分中?

class MyTableViewCell: UITableViewCell {

    weak var delegate: MainViewController?
    @IBOutlet weak var myCollectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        myCollectionView.delegate = self
        myCollectionView.dataSource = self
        // Initialization code
    }
}

extension MyTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return MainViewController.dishesCountByArray[MainViewController.indexOfTableView!]
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SelectedCellViewController") as! SelectedCellViewController
        print(indexPath.section)
        print(indexPath.row
        // I Need That piece of code here for sending it 
        self.delegate?.navigationController?.pushViewController(vc, animated: true)

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.myCollectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
        if MainViewController.indexOfTableView == 0 {
            for index in 0...MainViewController.dishesArrayCount {
                if MainViewController.indexedDishesDictionary[index] == "Plav" {
                    cell.cellImage.image = UIImage(named: MainViewController.dishesArray[index].image)
                    MainViewController.indexedDishesDictionary.removeValue(forKey: index)
                    break
                }
            }
        } else if MainViewController.indexOfTableView == 1 {
            for index in 0...MainViewController.dishesArrayCount {
                if MainViewController.indexedDishesDictionary[index] == "Sup" {
                    cell.cellImage.image = UIImage(named: MainViewController.dishesArray[index].image)
                    MainViewController.indexedDishesDictionary.removeValue(forKey: index)
                    break
                }
            }
        } else if MainViewController.indexOfTableView == 2 {
            for index in 0...MainViewController.dishesArrayCount {
                if MainViewController.indexedDishesDictionary[index] == "Tort" {
                    cell.cellImage.image = UIImage(named: MainViewController.dishesArray[index].image)
                    MainViewController.indexedDishesDictionary.removeValue(forKey: index)
                    break
                }
            }
        } else { return cell }
        return cell
    }
}

1 个答案:

答案 0 :(得分:-1)

创建一个UICollectionView子类并添加一个IndexPath变量来存储tableView indexPath

class MyCollectionView: UICollectionView {
    var tableViewIndexPath: IndexPath?
    //...
}

MyTableViewCell

中使用此集合视图类
class MyTableViewCell: UITableViewCell {    
    @IBOutlet weak var myCollectionView: MyCollectionView!
    //...
}

tableView cellForRowAt中的tableViewIndexPath属性中设置索引路径

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as? MyTableViewCell
    cell?.myCollectionView.tableViewIndexPath = indexPath
    return cell
}

collectionView didSelectItemAt中,您可以获得表视图的indexPath

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let collectionView = collectionView as? MyCollectionView {
        print("Tableview section \(collectionView.tableViewIndexPath?.section) row \(collectionView.tableViewIndexPath?.row)")
    }
}