Swift CustomCell (TableView) 中的按钮将参数传递给 targetMethod

Swift Button inside CustomCell (TableView) passing arguments to targetMethod

我的 TableView 具有自定义单元格的功能,这些单元格有一个按钮可以在另一个视图中显示相应的详细信息。 这个线程让我开始了,我尝试用 customCell 中的委托来实现这个方法:

我想要实现的是,当我单击按钮时,它会读取单元格的名称并将其传递给下一个控制器。但是,似乎我无法使用委托方法传递名称,它的字段是 nil.

如何在单击按钮时获取单元格的具体内容?

这是我目前所做的:

在 class 创建我自己的单元格时,我设置了委托:

protocol CustomCellDelegate {
 func cellButtonTapped(cell: DemoCell)
}
(........)
 var delegate: CustomCellDelegate?

@IBAction func buttonTapped(sender: AnyObject) {
    delegate?.cellButtonTapped(self)
}

TableViewController 我有以下内容:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {
    let cell =
        tableView.dequeueReusableCellWithIdentifier("FoldingCell",
                                                    forIndexPath: indexPath) as! DemoCell
    cell.delegate = self

 //TODO: set all custom cell properties here (retrieve JSON and set in cell), use indexPath.row as arraypointer

    let resultList = self.items["result"] as! [[String: AnyObject]]
    let itemForThisRow = resultList[indexPath.row]

    cell.schoolNameClosedCell.text = itemForThisRow["name"] as! String
    cell.schoolNameOpenedCell.text = itemForThisRow["name"] as! String

    self.schoolIdHelperField = itemForThisRow["name"] as! String

    cell.schoolIntroText.text = itemForThisRow["name"] as! String

  //call method when button inside cell is tapped
    cell.innerCellButton.addTarget(self, action: #selector(MainTableViewController.cellButtonTapped(_:)), forControlEvents: .TouchUpInside)

    cell.school_id = itemForThisRow["name"] as! String

  //  cell.schoolIntroText.text = "We from xx University..."
    return cell
}

最后是单击单元格内的按钮时的目标方法

func cellButtonTapped(cell: DemoCell) {
    print("the school id: ")
    print(cell.schoolNameOpenedCell) //this line throws an error EXC_BAD_ACCESS 0x0

}

首先,对象innerCellButton不是一个Cell,它是一个按钮。解决问题的简单方法是,只需参考按钮的索引即可。请找到以下方法。

func cellButtonTapped(AnyObject: sender) {

  let resultList = self.items["result"] as! [[String: AnyObject]]
  //Get the tag value of the selected button. 
  //Button tag should be matching with the corresponding cell's indexpath.row
  let selectedIndex = sender.tag
  let itemForThisRow = resultList[selectedIndex]
  print("the school id: \(itemForThisRow[\"name\"])")

}

* 并将每个按钮的标签设置为 indexPath.row *

例如,

override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {

// Dequeue your cell and other code goes here.

// set the button's tag like below.
cell.innerCellButton.tag = indexPath.row
return cell
}

关闭。我不会使用 Suresh 的方法,因为它无助于找到包含部分和行的 IndexPath。

首先,我会为您的 table 视图数据源推荐一个模型对象。详细了解 MVC 模式以及解析 JSON 对具有映射的对象的响应。但是,这将为您提供所需的数据。

func cellButtonTapped(cell: UITableViewCell) {
    let indexPath = tableView.indexPathForCell(cell)
    let resultList = self.items["result"] as! [[String: AnyObject]]
    let itemForThisRow = resultList[indexPath.row]
    let name = itemForThisRow["name"] as! String
}