【问题标题】:Is it possible to show table view cell index path and collection View Index path in swift?是否可以快速显示表格视图单元格索引路径和集合视图索引路径?
【发布时间】:2020-07-07 10:14:56
【问题描述】:

我想知道如何显示表视图和集合视图索引的索引路径。在我的情况下,表格视图单元格包含许多集合视图单元格。并且集合视图单元格包含一个按钮,当用户按下此按钮时,会显示一个警报显示,该按钮显示来自表视图索引(即 2)和集合视图索引(即 4)的按下按钮。

这是怎么做到的?

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
  
    }
    //MARK:- Tabel View
       
       func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return 10
       }
       
       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellForTableView
        
        return cell
       }
    //MARK:- Collectio View
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CellForCollectionView
        cell.myIndexPath = indexPath
        cell.TabedDelegate = self
        
        return cell
    }
}
extension ViewController : collectionViewCellTabbedDelegate{
    func clickBtnTabbed(indexPath: IndexPath) {
        
        print(indexPath)
                   
               let alert = UIAlertController(title: "Show Index Number!", message: "Selected Collection View Index Number is \(indexPath[1])", preferredStyle: .alert)
                       let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                       alert.addAction(okAction)
               
                       self.present(alert, animated: true, completion: nil)  
        
    }
    
    
}

这是我的收藏视图单元格:

import UIKit

protocol collectionViewCellTabbedDelegate {
    func clickBtnTabbed(indexPath: IndexPath)
}


class CellForCollectionView: UICollectionViewCell {
    var myIndexPath : IndexPath!
    var TabedDelegate : collectionViewCellTabbedDelegate?
    
    @IBOutlet weak var clickBtnOut: UIButton!
    
    
    override func awakeFromNib() {
           super.awakeFromNib()
           
           
           self.clickBtnOut.addTarget(self, action: #selector(clickBtnAct(_:)), for: .touchUpInside)
           
           
       }
     
    @IBAction func clickBtnAct(_ sender: Any) {
        TabedDelegate?.clickBtnTabbed(indexPath: self.myIndexPath)
    }
    
    
    
    
    
}

【问题讨论】:

  • 拜托,Stackoverflow 不是代码编写服务。显示您目前拥有的代码。
  • @vadian 现在请给我正确的解决方案

标签: ios swift uitableview uicollectionview


【解决方案1】:

我不知道您的认识,但从我的角度来看,我建议使用 ColletionView 而不是 TableView,因为它更灵活。据我了解,您在表格视图单元格中有集合视图。要获取 IndexPath,您可以使用委托,在点击单元格后,您可以通过委托传递您的 indexPath。

如果你想访问单元格内的单元格的 indexPath,你可以尝试访问你的超级视图(签出 Stackoverflow)或者你可以使用下面的代码

extension UIResponder {
    func next<U: UIResponder>(of type: U.Type = U.self) -> U? {
        return self.next.flatMap({$0 as? U ?? $0.next() })
    }
}

extension UICollectionViewCell {
    var collectionView: UICollectionView? {
        return self.next(of: UICollectionView.self)
    }

    var indexPath: IndexPath? {
        return self.collectionView?.indexPath(for: self)
    }
}

【讨论】:

    【解决方案2】:

    以自己的方式自定义这些代码

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return count
                }
                
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    
                    let cell:TypeCell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! TypeCel 
                    cell.CollectVw.dataSource = self
                    cell.CollectVw.delegate = self
                    cell.CollectVw.tag = indexPath.row
                    cell.CollectVw.reloadData()
                    return cell
                }
            func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                    let dt = array[collectionView.tag] as! NSArray
                    return dt.count
                }
            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                    
                    let cell:collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! collectionCell
                   
                    let datGpAry = array[collectionView.tag] as! NSArray
                    let dat = datGpAry[indexPath.row] as! NSDictionary
            cell.button.addTarget(self, action: #selector(btnClk), for: .touchUpInside)
                    cell.button.tag = collectionView.tag
                    cell.button.accessibilityValue = String(indexPath.row)
                    
                    return cell
                }
        @objc func btnClk(sender:UIButton) {
                let index = sender.accessibilityValue!
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多