In my storyboard, I have a UITableViewController (embedded in a UINavigationController) on which I have added a CANCEL button as a Left Bar Button Item.
At runtime, the CANCEL button doesn't appear, but an EDIT button does.
I haven't explicitly added the EDIT button and have kept the default implementation of the edit function in its disabled state thus:
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return false
}
I've looked into ways to hide the EDIT button and this line of code works:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.leftBarButtonItem = nil
}
... in that it hides the EDIT button. But, it also hides the CANCEL and all other buttons on the left!
I have discovered code that explicitly shows the EDIT button, i.e.:
self.navigationItem.leftBarButtonItem = self.editButtonItem
But cannot find an equivalent for CANCEL.
So, is there a way to hide the EDIT button but show the CANCEL button?
--- UPDATE #1 ---
I have already connected the Cancel button to my class code via an IBOutlet per attached screenshot.
--- UPDATE #2 ---
I have now ensured that
tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath)
and
tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
are commented out. So, as far as I can tell, there is nothing about this TableViewController that enables editing of rows. Yet, the 'EDIT' button still appears and obfuscates my 'CANCEL' button.
leftBarButtonItems is a slightly different thing than leftBarButtonItem
It sounds like your cancel button has been added to the leftBarButtonItems of the navigationItem for your view controller, but you don't have a reference to it in your class. Create an IBOutlet reference for it and you should be able to set it explicitly as the leftBarButtonItem in the same way as you are the edit button.
Facepalm time.
I had copied over a method from another class which had the edit button explicitly displayed the following line:
self.navigationItem.leftBarButtonItem = self.editButtonItem
Deleting this line removes the EDIT button at run time.
Related
How can I change the title of a custom navigation bar button when the "delete" button of a table row that is under editing, is either tapped or dismissed?
The button is the "editBarButton". It is a custom navigation bar button. The initial title of it is "Edit". It must remain as "Edit" while the table is in non-editing mode, and it must change to "Done" when the table is in editing mode. Therefore, when a table row is swiped so as the "delete" appears, the title of the "editBarButton" must be "Done"; it must be "Edit" when the title leaves the editing mode, either if the "delete" is tapped or dismissed (i.e., dismissed by tapping the row under deletion anywhere else except from the "delete" button). Using the code below, the title changes to "Done" when a row is swiped and the "delete" is appearing. But I cannot find a way to make it have the "Edit" title again.
The "editBarButton" is connected with #IBAction func editBarButtonIsPressed(_ sender: Any), as shown below. Therefore, each time the button is tapped, the title of it changes (as well as the value of tableView.isEditing). The title of it is also set to "Done" when a swipe is applied on a row of the table. However, I cannot find a way to make its title change back to "Edit" when the "delete" button (that appears after the swipe) is tapped or dismissed.
#IBAction func editBarButtonIsPressed(_ sender: Any)
{
if tableView.isEditing { editBarButton.title = "Edit" }
else { editBarButton.title = "Done" }
tableView.setEditing(!tableView.isEditing, animated: true)
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
editBarButton.title = "Done"
return .delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete
{
//here, the element of the array that is presented in the relevant row of the table, is deleted, as well as the row
}
}
I have found the answer. The following function must be used:
override func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?)
{
editBarButton.title = "Edit"
}
I want to customise the delete indicator for a UITableView in editing mode.
Since I can't seem to find a way to customise the appearance style of the delete button shown in the first image, I've created my own animation with my own button.
I have deactivated the red standard button using:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
Now my question: How do I realize the same behaviour as the standard button? If you press the red button, it invokes a swipe left on the cell showing the wanted delete button.
Help is very appreciated.
I have a custom table view cell that has a toggle button on the left side.
When the tableview's isEditing property is set to true, the delete editing option appears properly, but when it's tapped, nothing happens.
The stranger thing is, when I tap and hold, and then drag to one side away from the button, and THEN lift, the cell finally slides over. Which isn't how it's supposed to work at all.
Even when I remove connections to the toggle button from the storyboard and the subclass, it still behaves this way.
Why is it doing this?
First of all, correct me if I'm wrong, but I think these are two different problems.
1.Nothing happens when the "delete editing" button is tapped. I would check if the "delete editing" button properly connected to the class file with an IBAction.
2.The cell slide behavior is not what is desired. I would check to see if the gesture you're recognizing is a tap and not a left swipe.
Hope that helps.
you need to add following methods in your table view controller
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
//remove element from your array providing index.row
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
)
}
}
Basically I have a tableView in my viewController showing some calendar events and upon clicking a row I want to show the default EKEventViewController that presents the event in a standard way.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let eventViewController = EKEventViewController()
eventViewController.event = lastEvents[indexPath.row]
eventViewController.allowsCalendarPreview = true
show(eventViewController, sender: nil)
}
This works, however there's a strange dark background that appears "under" this EKEventViewController. I have to get rid of this background.
I've uploaded an example application that exhibits this behavior.
Additionally, after the EKEventViewController has been shown and popped, there remains a bar at the bottom (where the "Delete Event" button was).
Edit: I forgot to mention that there's a debug message telling me Unable to simultaneously satisfy constraints. However it's strange since it's not really my layout. All I'm doing is showing this viewController.
Here's how it looks:
I have a UITableView, which comes prefitted with an Edit button (UITableView.editButtonItem()). When pressed, it toggles to "Done", and you get those red minus signs next to entries for when you want to delete them.
I want to attach custom code to the button so that when you click it, you don't get the red minus signs. When in edit mode, whenever you click on an entry, it brings up a UIActionSheet or a UIAlertController to allow you to make the changes.
So, I want to override the Edit button so that when clicked:
The red minus "Delete" icons don't show up
The cells become selectable.
I've tried creating a custom UIBarButtonItem, but that's a terrible walkaround. I think what I want is do-able, I'm just not sure how.
Use delegate for UIViewController and make custom code on the events of that button. If it is on every row you must attach tap action from the code during cell generation.
I myself would preffer to have custom class.
I've figured out which methods to call:
// The following two functions remove the red minus sign
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
// The following property allows you to select cells while in editing mode
self.tableView.allowsSelectionDuringEditing = true