【问题标题】:How to obtain the cell index selected in UICollectionView如何获取UICollectionView中选中的单元格索引
【发布时间】:2016-09-20 02:45:32
【问题描述】:

我有一个 uitableview,其中一个单元格包含一个 UICollectionView。我想在 UICollectionView 中获取选定单元格的索引,但我无法,我试图搜索网络但没有找到我找到的内容。

【问题讨论】:

    标签: uicollectionview swift3 tableviewcell


    【解决方案1】:

    结果:


    故事板:

    视图控制器:

    我创建了一个闭包 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 作为 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)
        }
      }
    }
    

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

    【讨论】:

      【解决方案2】:

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

      1. 你有 ViewController,它有 tableView,你的 tableViewCell 里面有 CollectionView。
      2. 因此,当 CollectionView 的 Cell 被选中时,您需要将事件发送到 ViewController

      在您的 CustomTableViewCell 类中,您将拥有以下 CollectionView 委托方法。所以你可以在那里调用你的委托方法,它将事件发送到你的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
          }
      }
      

      【讨论】:

      • 非常感谢您的回答 Natarajan!但我需要的是调用一个视图控制器单击 CollectionView 中的一个单元格
      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多