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
Related
I am using the method
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//Do something
})
delete.backgroundColor = UIColor.redColor()
let more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//Do something
})
more.backgroundColor = UIColor.blueColor()
return [delete, more]
}
I also included
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
and when I swipe right or left the buttons I created are not showing up. I tried putting override on the editactionforrowatindexpath but then i get an error saying method does not override any method from its super class
There is nothing wrong in your implementation. The function is not getting called is because the delegate and datasource methods are not called properly.
You can connect the delegate and datasource methods by programatically or via storyboard.
When you are implement the tableview programatically You need to add UITableViewDataSource, UITableViewDelegate in class declaration and connect the delegates using this code.
yourTableView.datasource = self
yourTableView.delegate = self
If you are implement tableview through storyboard you just need to connect the delegate and data source method by dragging
I have this code and I need call a function when all buttons are show.
How can I tell when the animation that shows the editing actions has finished?'
My code has two button, this is the code:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let confirm = UITableViewRowAction(style: .Normal, title: "Confirm") { action, index in
//CODE
}
confirm.backgroundColor = UIColor.greenColor()
let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
//code
}
delete.backgroundColor = UIColor.redColor()
return [delete,confirm]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
I have tableView in the UIViewController class. I have to implement swipeable when to swipe it should show EDIT DELETE, that two action. My problem is, it's going inside the method when I swipe, but it did not show those two actions.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .Default, title: "Edit") { action, index in
print("EDIT");
}
editAction.backgroundColor = UIColor.blueColor()
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
print("DELETE");
// self.confirmDelete();
}
deleteAction.backgroundColor = UIColor.redColor()
return [editAction,deleteAction]
}
I haven't implemented tableview controller. This tableview is part of my screen. Not full screen. I tried in the UITableViewController class, which was working
Any help please
Add this delegate method
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
I'm developing a small app using Swift, an UITableView to show custom cells, and then I would like to swipe to edit to do that I use the following code :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BasicCell
cell.Name.text = contacts[indexPath.row].Name
cell.Phone.text = contacts[indexPath.row].Phone
cell.ProfilPic.load(contacts[indexPath.row].ProfilPic!)
cell.ProfilPic.layer.masksToBounds = true
cell.ProfilPic.layer.cornerRadius = CGRectGetWidth(cell.ProfilPic.frame)/2.0
return cell
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?{
let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
print("delete")
}
let done = UITableViewRowAction(style: .Default, title: "Done") { action, index in
print("done")
}
return [delete, done]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
the problem is when I swipe a cell, buttons are not showing :
How we can do the left swipe and right swipe in swift2
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// print("Commit Editing Style \(editingStyle)")
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
}
Only available function to help you swipe from right to left.
Here is the example for it:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
println("more button tapped")
}
more.backgroundColor = UIColor.lightGrayColor()
let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
println("favorite button tapped")
}
favorite.backgroundColor = UIColor.orangeColor()
let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
println("share button tapped")
}
share.backgroundColor = UIColor.blueColor()
return [share, favorite, more]
}
Then it will look like:
If you want to swipe from left to right, you need to code yourself, or you can use this library here:
https://github.com/CEWendel/SWTableViewCell
It takes just one method to enable swipe to delete in table views: tableView(_:commitEditingStyle:forRowAtIndexPath:). This method gets called when a user tries to delete one of your table rows using swipe to delete, but its very presence is what enables swipe to delete in the first place – that is, iOS literally checks to see whether the method exists, and, if it does, enables swipe to delete.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
objects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}