【问题标题】:Access selected cell section in delegate function in swift快速访问委托函数中的选定单元格部分
【发布时间】:2018-10-25 04:44:46
【问题描述】:

我正在从 API 服务获取数据,我将这些数据传递给我的 tableview 并在其下创建部分和单元格。部分和单元的数量是动态的,取决于来自服务的数据。我的手机上有一个按钮。按钮名称为添加。当我单击添加按钮时,它会显示一个包含表格视图的警报。警报中的此表视图仅显示与其部分下的特定单元格相关的数据。我为按钮创建了一个委托方法,在该方法中我获取了该选定按钮的 indexPath.row 并将数据从我的模型传递到警报内的表视图。当我单击第一个单元格添加按钮时,它显示一切都很好,但是当我从第 2 部分中单击添加按钮时,它会兑现。我观察到该应用程序正在崩溃,因为编译器仅获取 indexPath.row 但它没有获取有关此单元格是哪个部分的信息。当按下添加按钮时,我如何才能知道我的委托函数选择了哪个部分单元格。这是我的单元类中委托函数的代码,

protocol ResMenuDetailDelegate {
func addOnBtnTapped(tappedIndex : Int)
}

 var delegate: ResMenuDetailDelegate?

 @IBAction func addBtnTapped(_ sender: Any) {
    delegate?.addOnBtnTapped(tappedIndex: addBtn.tag)
}

在我的视图控制器类中,我符合委托方法,

extension RestaurantMenuDetailVC : ResMenuDetailDelegate{
func addOnBtnTapped(tappedIndex: Int) {

    print(tappedIndex)

    let addonCategory = subCategoryModel![tappedIndex].items[tappedIndex].addonCategory
  print(addonCategory as Any)
}

这是我的 cellForRow 表视图委托,

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == resMenuTableView{

        let cell = resMenuTableView.dequeueReusableCell(withIdentifier: "detailMenuCell", for: indexPath) as! RestaurantMenuDetailTVC

        cell.dishTitleLbl.text = subCategoryModel![indexPath.section].items[indexPath.row].itemName
        cell.descriptionLbl.text = subCategoryModel![indexPath.section].items[indexPath.row].itemDescription
        cell.priceLbl.text = String(subCategoryModel![indexPath.section].items[indexPath.row].itemPrice)
        cell.addBtn.tag = indexPath.row
        cell.delegate = self
        cell.selectionStyle = .none
        cell.backgroundColor = UIColor.clear
        return cell

    }

【问题讨论】:

  • 你会在 indexpath 中得到section和row,它是包含row和section的结构
  • 但是我的崩溃与错误索引超出范围。 @RahulGUsai

标签: ios swift uitableview


【解决方案1】:

像这样更改协议,以便您可以同时拥有两个值 行和部分。

    protocol ResMenuDetailDelegate {
    func addOnBtnTapped(tappedIndexRow: Int,tappedIndexSection: Int )
    }
    var delegate: ResMenuDetailDelegate?

 @IBAction func addBtnTapped(_ sender: Any) {
    delegate?.addOnBtnTapped(tappedIndexRow: addBtn.tag,tappedIndexSection: section )
}

这里可以获取section值tableview的delegate方法 cellForRowAt。 在自定义单元格中为部分添加变量

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        ...
        ...
        cell.addBtn.tag = indexPath.row
        cell.section = indexPath.section
        ...
        ...
    }

【讨论】:

    【解决方案2】:

    您需要发送 indexpath ,崩溃是因为您访问的数组模型部分的行值超过了它

    func addOnBtnTapped(tappedIndex : IndexPath)
    

    //

    extension RestaurantMenuDetailVC : ResMenuDetailDelegate{
     func addOnBtnTapped(tappedIndex: IndexPath) {
    
       print(tappedIndex)
    
       let addonCategory = subCategoryModel![tappedIndex.section].items[tappedIndex.row].addonCategory
     print(addonCategory as Any)
    }
    

    //

     @IBAction func addBtnTapped(_ sender: Any) {
       delegate?.addOnBtnTapped(tappedIndex:self.myIndexPath)
    }
    

    //

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    if tableView == resMenuTableView{
    
        let cell = resMenuTableView.dequeueReusableCell(withIdentifier: "detailMenuCell", for: indexPath) as! RestaurantMenuDetailTVC
    
        cell.myIndexPath = indexPath 
     }
    

    //

    并在单元格中声明该变量

    var myIndexPath:IndexPath!
    

    【讨论】:

    • 在单元格类的那个按钮的动作中写什么? @Sh_Khan
    • 让我检查一下。 @Sh_Khan
    • @Awais 您可以在 cell.tag 中发送部分并在 cell.btn.tag 中发送行
    • 它工作得很好,现在它没有崩溃。十分感谢 :) 。 @Sh_Khan
    【解决方案3】:

    我认为你必须在协议中传递两个参数。

    protocol ResMenuDetailDelegate {
    func addOnBtnTapped(tappedIndex : Int, button: UIButton)
    }
    

    【讨论】:

    • 这个按钮包含什么? @Khushbu
    • 此按钮包含 UIButton。这意味着它将定义特定的按钮。正如你得到的索引。您还应该获得正在执行操作的按钮。
    • 假设我写按钮,但它如何知道该按钮的部分? @Khushbu
    • 与您识别索引的方式相同。
    • 我正在通过该按钮的标签识别索引,我如何通过标签中的部分? @Khushbu
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 2018-10-09
    • 2023-03-12
    • 2020-03-31
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多