【问题标题】:how to get main tableview index in nested tableview in swift如何快速获取嵌套表视图中的主表视图索引
【发布时间】:2021-08-09 18:59:58
【问题描述】:

我是 swift 新手,我无法在嵌套的 tableview 按钮单击上获取主 tableview 索引

我的代码是这样的

extension Section2QuestionsViewController: UITableViewDataSource, UITableViewDelegate
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrFinancialYears.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let strCellID = "Section2QuestionCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
        if cell == nil
        {
            tableSection2.register(Section2QuestionCell.self, forCellReuseIdentifier: strCellID)
            cell = tableSection2.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
        }
        
        let dictAppStats =  arrFinancialYears[indexPath.row] as? [String:Any]
        cell?.lblQuestionTitle.text = "\(indexPath.row + 1). \(dictAppStats?["question"] as? String ?? "")"

        cell?.arrCourses = [Any]()
        cell?.arrCourses = dictAppStats?["options"] as? [Any] ?? []
        
        cell?.tableInsideHeightConstraints.constant = CGFloat(28 * (cell?.arrCourses.count ?? 0))
        
        cell?.tableInside.reloadData()
        

        
        return cell!
    }
}

表格视图内

extension Section2QuestionCell: UITableViewDataSource, UITableViewDelegate
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrCourses.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "InsideSection2QuestionCell", for: indexPath) as! InsideSection2QuestionCell
        
        cell.selectionStyle = .none
        let dictFeeStats = arrCourses[indexPath.row] as? [String:Any]
        cell.lblOption.text = dictFeeStats?["option_value"] as? String
        cell.btnOption.tag = indexPath.item
        cell.btnOption.addTarget(self, action: #selector(btnOptionClick), for: .touchUpInside)
        cell.lblAnswerKey.text = dictFeeStats?["answer_key"] as? String
        cell.lblQuestionId.text = dictFeeStats?["question_id"] as? String
        cell.lblAnswerId.text = dictFeeStats?["option_id"] as? String

        return cell
    }
}

按钮点击

@objc func btnOptionClick(_ sender: UIButton)
    {
        let index = IndexPath(row: sender.tag, section: 0)
        let cell: InsideSection2QuestionCell = tableInside.cellForRow(at: index) as! InsideSection2QuestionCell

        let QuestionId = cell.lblQuestionId.text
        let AnswwerId = cell.lblAnswerId.text
        
        print(QuestionId as Any)
        print(AnswwerId as Any)
        
        Globalnewdict = ["option":AnswwerId as Any,"question":QuestionId as Any]
        Globalindexvalue = sender.tag
        
        NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)

       tableInside.reloadData()
    }

我无法在按钮上获取主 tableview 索引路径值。有什么方法可以在单击按钮时获取主 tableview 索引路径值。

请帮忙 提前致谢!

【问题讨论】:

  • 您有表格视图单元格,每个单元格中都包含一个表格视图?
  • 您在表格视图单元格中有一个按钮,并且您想在单击该按钮时获取单元格的索引路径,这是您的目的吗?如果不是,请澄清您的问题。

标签: ios swift uitableview


【解决方案1】:

您可以在 Section2QuestionCell 中添加一个索引路径对象

var parentTableIndexPath : IndexPath!

然后在cellForRowAt tableview委托方法中设置值

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let strCellID = "Section2QuestionCell"
    var cell = tableView.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
    if cell == nil {
        tableSection2.register(Section2QuestionCell.self, forCellReuseIdentifier: strCellID)
        cell = tableSection2.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
    }
    cell.parentTableIndexPath = indexPath
    let dictAppStats =  arrFinancialYears[indexPath.row] as? [String:Any]
    cell?.lblQuestionTitle.text = "\(indexPath.row + 1). \(dictAppStats?["question"] as? String ?? "")"
    cell?.arrCourses = [Any]()
    cell?.arrCourses = dictAppStats?["options"] as? [Any] ?? []
    cell?.tableInsideHeightConstraints.constant = CGFloat(28 * (cell?.arrCourses.count ?? 0))
    cell?.tableInside.reloadData()
    return cell!
}

由于 btnOptionClick() 属于同一类,您可以在按钮操作中直接访问父表索引路径。

【讨论】:

    【解决方案2】:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let strCellID = "Section2QuestionCell"
            var cell = tableView.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
            if cell == nil
            {
                tableSection2.register(Section2QuestionCell.self, forCellReuseIdentifier: strCellID)
                cell = tableSection2.dequeueReusableCell(withIdentifier: strCellID) as? Section2QuestionCell
            }
            
            let dictAppStats =  arrFinancialYears[indexPath.row] as? [String:Any]
            cell?.lblQuestionTitle.text = "\(indexPath.row + 1). \(dictAppStats?["question"] as? String ?? "")"
    
            cell?.arrCourses = [Any]()
            cell?.arrCourses = dictAppStats?["options"] as? [Any] ?? []
            
            cell?.tableInsideHeightConstraints.constant = CGFloat(28 * (cell?.arrCourses.count ?? 0))
             cell?.tableInside.tag = indexPath.row
            cell?.tableInside.reloadData()
            
    
            
            return cell!
        }
    

    在按钮点击中

    @objc func btnOptionClick(_ sender: UIButton)
        {
            let index = IndexPath(row: sender.tag, section: 0)
            let cell: InsideSection2QuestionCell = tableInside.cellForRow(at: index) as! InsideSection2QuestionCell
    
            let QuestionId = cell.lblQuestionId.text
            let AnswwerId = cell.lblAnswerId.text
            
            print(QuestionId as Any)
            print(AnswwerId as Any)
            
            Globalnewdict = ["option":AnswwerId as Any,"question":QuestionId as Any]
            Globalindexvalue =  tableInside.tag
            
            NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)
    
           tableInside.reloadData()
        }
    

    在 cellForRowAt 中,我将 indexpath.row 设置为 tableview.tag。 然后在按钮上单击我将 tableview.tag 设置为全局索引路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2020-11-20
      • 1970-01-01
      相关资源
      最近更新 更多