ios - 在 swift 中访问委托(delegate)函数中选定的单元格部分

标签 ios swift uitableview

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

protocol ResMenuDetailDelegate {
func addOnBtnTapped(tappedIndex : Int)
}

 var delegate: ResMenuDetailDelegate?

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

在我的 View Controller 类中,我遵循委托(delegate)方法,

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

    print(tappedIndex)

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

这是我的 cellForRow TableView 委托(delegate),

  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,崩溃是因为您访问数组模型部分的行值超过了它

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!

关于ios - 在 swift 中访问委托(delegate)函数中选定的单元格部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50348682/

相关文章:

ios - 滚动完成时居中单元格 UITableView

ios - UITableView 方法在 ViewDidLoad 之前调用

ios - iOS 邮件应用程序撰写屏幕是否使用 UITableView?如果是这样,为什么?

ios - 将两个可变宽度 View 保持在 super View 中心的约束

ios - scrollView.setContentOffset 在 iOS 12 上的 scrollView.contentInset 之后不起作用

ios - Swift 中的自动换行标签

swift - 如果一个函数返回一个 UnsafeMutablePointer,我们有责任销毁和释放吗?

swift - 阻止应用程序备份文档文件夹中的文件?

ios - 更改 NSObject 定义导致无法识别的选择器

ios - UISearchController 搜索两个数组