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.
}
}
Related
How to display the uitableview's swipe option on didselectRow:
want to display swipe option like below.
exmaple show in fig, this should implement in didselect row. How can achive this help me. Thanks advance.
First , you should sure that:
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
}
and second you will implementate in this delegate method:
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]
}
the finall view like that:
hope this is helpful for you.
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
}
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
In my app I have a tableView in witch I have default text and I would like to have a swipe to edit text function so the user can change the text if the want to.
I already added the swipe to delete, using the code below, however adding an editing text function isn't as easy to find information on, So my question is, How can I add a button so the user can edit the text via a swipe function?
I need to know the code to load the text as I can't have a textfield so it's not as easy as something like label.text = textfield.text
the code that original loads the text is as follows
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = places[indexPath.row]["name"]
Thanks !
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
places.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
tableView.reloadData()
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
var editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (action, indexPath) in
tableView.editing = false
// your action
}
editAction.backgroundColor = UIColor.grayColor()
var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) in
tableView.editing = false
// your delete action
}
return [deleteAction, editAction]