UICollectionView中如何获取选中的单元格索引

How to obtain the cell index selected in UICollectionView

我有一个 uitableview,其中一个单元格包含一个 UICollectionView。我想获取 UICollectionView 中选定单元格的索引,但我做不到,我尝试搜索网络但没有找到我找到的结果。

如果我没有正确理解你的问题,你会有以下需求。

  1. 您有 ViewController,它有 tableView,在您的 tableViewCell 里面有 CollectionView。
  2. 所以您需要在选择 CollectionView 的 Cell 时将事件发送到 ViewController

在您的 CustomTableViewCell class 中,您将拥有以下 CollectionView Delegate 方法。所以你可以调用你的委托方法,它将事件发送到你的 ViewController。首先在您的 CustomTableViewCell 中添加一个委托变量,如下所示。

protocol CustomCellDelegate {
    func selectedItem(_ item:Int)
}

class CustomTableViewCell: UITableViewCell
{
    weak var delegate:CustomCellDelegate?

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
           if delegate != nil
           {
               delegate?.selectedItem?( (indexPath as NSIndexPath).item)
           } 
    }
}

在您的视图控制器中只需添加 CustomCellDelegate

class ViewController:UIViewController, CustomCellDelegate{

func selectedItem(_ item:Int){
//Your stuffs
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
          let cell://dequeue your cell here
          cell.delegate = self //This delegate will send the actions from UICollectionView
          return cell
    }
}

结果:


故事板:

ViewController:

我创建了一个闭包 didCollectionViewCellSelect 并将其传递给 TableViewCell 以便当来自 UICollectionView[= 的单元格时46=] 完成后,将调用此闭包。

class ViewController: UIViewController {

  @IBOutlet weak var tableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
  }

  lazy var didCollectionViewCellSelect: ((Int) -> Void)? = { (collectionViewCellIndex) in
    self.performSegue(withIdentifier: "showDetail", sender: collectionViewCellIndex)
  }

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {
      if let collectionViewCellIndex = sender as? Int {
        let detailViewController = segue.destination as! DetailViewController
        detailViewController.collectionViewCellIndex = collectionViewCellIndex
      }
    }
  }
}


extension ViewController: UITableViewDelegate, UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
  }

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

    cell.didCollectionViewCellSelect = didCollectionViewCellSelect

    return cell
  }
}

TableViewCell:

这里的重要部分是 TableViewCell 将作为 UICollectionView 的委托作为 delegatedataSource 以便 TableViewCell 将在例如选择单元格时得到通知。

class TableViewCell: UITableViewCell {

  @IBOutlet weak var collectionView: UICollectionView!

  var didCollectionViewCellSelect: ((Int) -> Void)?

  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    collectionView.delegate = self
    collectionView.dataSource = self
  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
  }

}

extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)

    return cell
  }

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let didCollectionViewCellSelect = didCollectionViewCellSelect {
      didCollectionViewCellSelect(indexPath.row)
    }
  }
}

如果您需要我所做的更多解释,请告诉我,希望这对您有所帮助。