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
}
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 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'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 :
I implemented editActionsForRowAtIndexPath and commitEditingStyle the swipe is working but no action buttons are shown on the UITableViewCell
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// I did some work here
})
return [deleteAction]
}
Does anyone have an idea how to get the action buttons to show when sliding? (Currently, the cell slides but behind it is a white background with no action buttons. Also clicking this white background provokes no response of any kind)
Note: I implemented editActionsForRowAtIndexPath in another ViewController and it works. (The action buttons both appear and response to click events)
I had the same problem and in my case I realized that my custom cell was way to wide for the table view. I noticed this because the accessory was not shown. Once I ran it on the iPad Emulator, I saw the buttons.
The event should be commitEditingStyle here to use swipe button actions
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
commitEditingStyle is empty because it is required for sliding. editActionsForRowAtIndexPath is implemented and this works in one viewController but does not work for another
The code is as follow
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//some code here
})
return [deleteAction]
}