我有一个CustomCell类,它有一个按钮。我正在使用原型单元格(不是.xib)。我想让tableviewcell中的按钮执行一个segue并将一个值传递给一个新类。我如何为tableviewcell中的按钮创建一个独特的动作?谢谢!从TableViewCell中的Button执行Segue
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
selectedAreaCode = areaCodes[indexPath.row]
// Configure Cell
cell.areaCodeButton.setTitle(areaCode, forState: UIControlState.Normal)
cell.areaCodeButton.addTarget(self, action: "segue", forControlEvents: UIControlEvents.TouchUpInside)
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func segue() {
self.performSegueWithIdentifier("toDialer", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "toDialer") {
let nextViewController = (segue.destinationViewController as! PhoneDialer)
nextViewController.passedAreaCode = selectedAreaCode
}
}
因此,您有多个“CustomCell”单元,每个单元都有一个按钮可以完成不同的操作? – gadu
使用'cell.button.tag = indexPath.row' – Lamar
如果您希望同一个表的单元格延伸到不同的视图,则必须为它们提供不同的重用标识符。 – orccrusher99