Reputation: 1389
I am using swipe gesture for tableview cell options.
This is my code:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Sil" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let shareMenu = UIAlertController(title: nil, message: "Emin misiniz ?", preferredStyle: .ActionSheet)
let twitterAction = UIAlertAction(title: "Sil", style: UIAlertActionStyle.Default, handler: self.deleteConversation)
let cancelAction = UIAlertAction(title: "İptal", style: UIAlertActionStyle.Cancel, handler: nil)
shareMenu.addAction(twitterAction)
shareMenu.addAction(cancelAction)
self.presentViewController(shareMenu, animated: true, completion: nil)
})
return [shareAction]
}
You can see in the code, i am using deleteConversation
method for handler. But I don't know how to get row id in deleteConversation
method.
This is my deleteConversation
method:
func deleteConversation(alert: UIAlertAction!) {
//I need the row id here
println("sill")
}
How can I get the row id in handler method ?
Upvotes: 0
Views: 254
Reputation: 131426
You have a couple of choices. You can save the row/indexPath of the edit action to a new instance variable at the beginning of your editActionsForRowAtIndexPath, and then look at that instance variable in your deleteConversation function. (You would need to create a new class-wide instance variable in your view controller for this option to work.)
Second option: You can pass in an inline block in your call to create the twitter action:
func tableView(tableView: UITableView,
editActionsForRowAtIndexPath
indexPath: NSIndexPath) -> [AnyObject]?
{
//skipped code here...
let twitterAction = UIAlertAction(
title: "Sil",
style: UIAlertActionStyle.Default,
handler:
{
(alert: UIAlertAction!) in
println("User asked to delete twitter feed at index \(indexPath.row)")
})
//skipped more code here...
}
With that syntax you are passing in a closure as the twitter action handler, and defining the closure in-line. When you do that the closure has access to the variables in it's enclosing scope. In this case it gives us access to the indexPath that's passed in.
Upvotes: 1
Reputation: 1580
Class YourClassNameHere
var cellIndexPath: NSIndexPath?
// Other stuff here...
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
cellIndexPath = indexPath
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Sil" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let shareMenu = UIAlertController(title: nil, message: "Emin misiniz ?", preferredStyle: .ActionSheet)
let twitterAction = UIAlertAction(title: "Sil", style: UIAlertActionStyle.Default, handler: self.deleteConversation)
let cancelAction = UIAlertAction(title: "İptal", style: UIAlertActionStyle.Cancel, handler: nil)
shareMenu.addAction(twitterAction)
shareMenu.addAction(cancelAction)
self.presentViewController(shareMenu, animated: true, completion: nil)
})
return [shareAction]
}
func deleteConversation(alert: UIAlertAction!) {
//Print row indexpath here
println("sill \(cellIndexPath.row)")
}
Upvotes: 1
Reputation: 4912
I assume by ID
you mean indexPath
?
In order to get it, have you tried using indexPathForSelectedRow
on self.tableView
?
Upvotes: 0