Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CustomCell(TableView)内的Swift按钮将参数传递给targetMethod_Ios_Swift_Uibutton_Tableview_Custom Cell - Fatal编程技术网

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

Ios CustomCell(TableView)内的Swift按钮将参数传递给targetMethod,ios,swift,uibutton,tableview,custom-cell,Ios,Swift,Uibutton,Tableview,Custom Cell,MyTableView具有自定义单元格,这些单元格有一个按钮,可以在另一个视图中显示相应的详细信息。 这里的这个线程让我开始了,我尝试在customCell中使用委托实现该方法: 我想要实现的是,当我点击按钮时,它读取单元格的名称并将其传递给下一个控制器。但是,我似乎无法用delegate方法传递名称,它的字段是nil 单击单元格按钮时,如何获取单元格的特定内容 这就是我目前所做的: 在创建我自己的单元格的类中,我设置了委托: protocol CustomCellDelegate { fu

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

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

单击单元格按钮时,如何获取单元格的特定内容

这就是我目前所做的:

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

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不是一个单元格,它是一个按钮。解决问题的简单方法是,只需参考按钮的索引。请找到下面的方法

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*

例如:


接近。我不会使用Suresh的方法,因为它无助于查找包含节和行的IndexPath

首先,我建议为表视图数据源提供一个模型对象。了解有关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
}

Thx,但这里的indexPath值为零。当然,在做了一些小改动后,DemoCell的方法还是有效的,所以我认为DemoCell应该适合我的方法。(删除子类参数)@user2039697Thx,这工作正常。我必须将协议签名:protocol CustomCellDelegate{func cellButtonTapped(cell:DemoCell)更改为(sender:AnyObject)},并将func cellButtonTapped(AnyObject:sender)更改为func cellButtonTapped(sender:AnyObject)
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
}