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

时间:2016-09-20 02:45:32

标签: uicollectionview swift3 tableviewcell

我有一个uitableview,其中一个单元格包含一个UICollectionView。我想在UICollectionView中获取所选单元格的索引而我无法使用,我试图搜索网页但是找不到我找到的工作。

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您将有以下要求。

  1. 你有ViewController,它有tableView,你的tableViewCell里面有CollectionView。
  2. 因此,当选择CollectionView的单元格时,您需要将事件发送到ViewController
  3. 在CustomTableViewCell类中,您将拥有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
        }
    }
    

答案 1 :(得分:0)

结果:

故事板:

enter image description here

<强>的ViewController:

我创建了一个闭包 didCollectionViewCellSelect 并将其传递给 TableViewCell ,以便在完成 UICollectionView 的单元格时,将调用此闭包

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 的委托,作为委托 dataSource 以便在选择单元格时通知 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)
    }
  }
}

如果您需要更多关于我的解释,请告诉我,希望您觉得这很有帮助。

相关问题