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]
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've previously implemented deletion for cells in my UITableView by using the following code:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
feedTable.deleteRows(at: [indexPath!], with: .fade)
}
}
However, now I want to add a custom action for the cells so I've added this:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let edit = UITableViewRowAction(style: .normal, title: "Edit", handler: { (action, indexPath) in
})
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
}
edit.backgroundColor = UIColor.green
return [delete, edit]
}
Now I'm confused whether I even need the original commit editingStyle function. Should I just move all my editing handling code (feedTable.deleteRows(at: [indexPath!], with: .fade)) to the new function?
It seems to me like there are many different functions that have to do with UITableViewCell editing and I'm confused about which ones to use when.
Should I just move all my editing handling code (feedTable.deleteRows(at: [indexPath!], with: .fade)) to the new function?
The answer to your question : Yes
When you implement the tableView( _: editActionsForRowAtIndexPath: ) method, the table view will no longer generate the Delete button for you. This is why you need to create your own Delete button.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let edit = UITableViewRowAction(style: .normal, title: "Edit", handler: { (action, indexPath) in
// your handling code
})
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// your handling code
}
// buttons colors
edit.backgroundColor = UIColor.green
delete.backgroundColor = UIColor.red
return [delete, edit]
}
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 :
I have a custom UITableViewCell where everything is displaying correctly and I am able to swipe each row to the left. On an iPad, everything works fine. However on any model of iPhone, rather than display the two edit actions as defined in the code below, nothing is displayed. The row simply slides to the left, leaving nothing where the edit buttons should be.
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let thing = things![indexPath.row]
var editAction = UITableViewRowAction(style: .Default, title: "Edit") { (action, indexPath) -> Void in
tableView.editing = false
self.editThing = thing
self.performSegueWithIdentifier("thingEdit", sender: self)
}
editAction.backgroundColor = UIColor.grayColor()
var deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (action, indexPath) -> Void in
tableView.editing = false
self.deleteThing(indexPath)
}
deleteAction.backgroundColor = UIColor.redColor()
return [editAction, deleteAction]
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
I am using essentially exactly the same code elsewhere and it works fine on all devices. I have confirmed that there are no visible differences between the definition of the working and non-working tables in the storyboard.