单击tableview单元格上的按钮时,我想将一些单元格数据返回到按钮动作功能
TableView代码:
var cell = self.myTable.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
var imgtext:AnyObject?
if (self.SearchController.active)
{
cell.textLabel?.text! = self.searchArray[indexPath.row]
cell.imageView?.image = decodedimage
let Acceptbutton : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
Acceptbutton.frame = CGRectMake(40, 60, 60, 24)
let cellHeight: CGFloat = 44.0
Acceptbutton.center = CGPoint(x: view.bounds.width / 1.4, y: cellHeight / 2.0)
Acceptbutton.backgroundColor = UIColor.greenColor()
Acceptbutton.addTarget(self, action: "AcceptClicked:", forControlEvents: UIControlEvents.TouchUpInside)
Acceptbutton.setTitle("Accept", forState: UIControlState.Normal)
cell.addSubview(Acceptbutton)
按钮操作:
@IBAction func AcceptClicked(sender: AnyObject)
{
//How can I get the cell.textlabel.text here for that specific
// cell row that AcceptClick was triggered from?
// I've tried sender.IndexPath but it returns nil
}
我试图将我的单元格数据传递到我的AcceptClicked函数中,这样我就可以对特定单元格进行一些操作来接受来自
的触发器答案 0 :(得分:2)
首先,您不希望IBAction
在函数前面,因为它不是指向Storyboard的链接。
其次,您可以使用:
func AcceptClicked(sender: AnyObject)
{
let thisButton = sender as! UIButton
var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position) {
//get your cell here
let cell = tableView.cellForRowAtIndexPath(indexPath)
}
}
如果只想要一行:
您可以在AcceptButton.tag = indexPath.row
中添加cellForRowAtIndexPath
并使用:
func AcceptClicked(sender: AnyObject)
{
let button = sender as! UIButton
row = button.tag
}