Hello,
I'm having trouble using the swipe to delete feature with Swift specifically, I implemented the following method to my table view but the delete button will not appear when I swipe across the simulator with the pointer. I tried to override the method but I got the following error saying,
override can only be specified on class members
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source Names.removeAtIndex(indexPath.row) Locations.removeAtIndex(indexPath.row) Types.removeAtIndex(indexPath.row) IsVisited.removeAtIndex(indexPath.row) Images.removeAtIndex(indexPath.row) print("Total Item: \\(Names.count)") for name in Names { print(name) }
instead of declaring variables for each property, I now define a Information
object to save all of the information. I replace the following variables:
var Image = ""
var Name = ""
var Type = ""
var Location = ""
with
var information: InformationSource!
Then I connected the label object from the object library in the prototype cell. I made a label for Field
and Value
, the field will hold Image, Name,Type, Location. Value will hold the values of each variable. Then I stacked both Field and Value horizontally
// Delete button
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: { (action, indexPath) -> Void in
// Delete the row from the data source
self.InformationSource.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
})
Try putting this :
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
override func tableView(tableView: UITableView,
editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let block = UITableViewRowAction(style: .Normal, title: "Block") { action, index in
print("Block")
self.removeObjectAtIndexPath(indexPath, animated: true)
}
let delete = UITableViewRowAction(style: .Default, title: "Delete") { action, index in
print("Delete")
self.removeObjectAtIndexPath(indexPath, animated: true)
}
return [delete, block]
}
Try to use existing solutions. https://github.com/CEWendel/SWTableViewCell It works fine for me.
Check swift implementation here: https://github.com/torryharris/TH-SwipeCell
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.