I made a project in interface builder with the Master-Detail Template and I'd like to get rid of the edit button.
I wrote (in MasterViewController):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
This disables the edit button, however the button is still there.
Then I tried (in the viewDidLoad, after connecting the tableView property to my MasterViewController class):
[self.tableView setEditing:NO];
However, the button is still there.
You need to remove the button altogether. setEditing has to do with wether or not the tableview is in edit mode or not, so try:
self.navigationItem.rightBarButtonItem = nil;
In your viewDidLoad method.
Also make sure you don't see a line of code in viewDidLoad that looks like:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
If yes, delete or comment it out.
Related
With reference to this question: Exit Edit Mode
about exiting edit mode when the last row is being deleted, my question is - how do you update the navigation bar "edit" item? After deleting the last row, I'd like to remove this nav bar item altogether AND exit edit mode (which is done per the question below) AND revert this button status back to "Edit" (rather than "Done" which is its status after deleting the last row).
Thats what I am doing now:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
...
if ([section count] == 0) { //last row in the section
[listOfItems removeObject:accessNumbers]; //updating my data source
tblSimpleTable.editing = NO; //added per the question above
// self.navigationItem.rightBarButtonItem = nil; --> thats what ideally i would want to do
// [self setEditing:YES animated:YES]; --> adding this manually doen't help
}
else
{
...
}
}
}
Thank you for the help!
UPDATE: adding this line doesn't help. I stil need to click on the nav bar item "Done" to exit the editing mode.
[self.tblSimpleTable setEditing:YES animated:YES];
if I also hide the nav bar item, I essentially can't exit the edit mode at all, and the screen is frozen (I have some other buttons on the view that simply dont react to touch anymore in that case).
According to the apple documentation, you cannot call [self.tableview setEditing:NO animated:YES] from within your tableView:commitEditingStyle:forRowAtIndexPath:. Here is the relevant excerpt:
Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.
Presumably one could then create a selector that turns off editing mode and removes the button.
Can you enforce this rule in another callback?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL answer = [section count] > 1;
self.navigationItem.rightBarButtonItem = (answer)? self.editButtonItem : nil;
return answer;
}
I was having issues doing the same thing (not able to get the status of the barButtonItem to revert to 'Edit' after calling setEditing) and learned from this answer https://stackoverflow.com/a/11490594/2888978 that the way to get change the 'Edit' back to 'Done' on the nave bar is to call setEditing on the view controller, not the table. Then you can set the barButtonItem to .None to remove it from the nav bar when the table is empty.
So instead of calling:
self.tableView.setEditing(false, animated: true)
You would call:
self.setEditing(false, animated: true)
Otherwise only the editing mode of the cells will change.
To leave editing mode, use this:
[self.tableview setEditing:NO animated:YES];
To remove the button all-together, use:
// Note that this only removes the right-most button. If you want to remove all of the buttons on the right side, use rightBarButtonItems instead.
self.navigationItem.rightBarButtonItem = nil;
// If you want it animated, use:
[self.navigationItem setRightBarButtonItem:nil animated:YES];
I am working on UITableView and I am doing a deletion. As far as I know there are 2 ways to delete a row
swiping from the left to right and Delete button will appears so that you can do a deletion
add EDIT button to right top right or left. By clicking on it, EDIT will change to DONE and each row of a table will have a red circle
with (-) sign in front of each. Click on it , delete button will
appear and you can process
I noticed that if you do have an EDIT button and use the method 1 to delete, the EDIT button also change to DONE...However, after swiping it, mine is still EDIT. does any one encounter this problem before, please help. All comments are welcomed here. Thanks
Swipe to delete happens when the view is not in edit mode. It's supposed to be a quick way to delete items when browsing the view. Check out how it works in the Mail app for example - you can either tap to edit all rows, or just swipe to delete one of them.
So basically it's working correctly.
However, if you want to override the standard behaviour, you can call setEditing: on the viewController to toggle the mode programmatically, which will also update the edit button.
Add the Edit button using the ViewdidLoad method
-(void)viewDidLoad;
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [self editButtonItem];
}
And for the delete method
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPath:[NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];
[self.tableView reloadData];
}
To get this behavior you need to implement these two delegate methods:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editing = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editing = NO;
}
Is there a possibility to reset a UISearchbar with searchDisplayController programmatically which is active and already contains text?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResultsTableView) {
searchDisplayController.searchResultsTableView.hidden = YES;
searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
}
}
This solution is working, but there is still text in the searchBar. If i add those lines of code:
searchBar.text = #"";
There is always a black transparent view left.
Any solutions?
Here ya go. This is a delegate method called when the user hits cancel. If you want to wire it up differently just declare your search bar as an outlet and reference it. Anyway:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = #"";
[searchBar resignFirstResponder];
}
Pretty sure what you're looking for is UISearchDisplayController.active
From the SDK:
#property(nonatomic, getter=isActive) BOOL active
If you set this value directly, any change is performed without animation. Use setActive:animated: if a change in state should be animated.
When the user focus in the search field of a managed search bar, the search display controller automatically displays the search interface. You can use this property to force the search interface to appear.
This thread is so old it has dust. Still here we are in swift so
<#your UISearchController variable#>.isActive = false
don't you have to retire the firstResponder
[self.searchBar resignFirstResponder]
You can also explicitly hide the resultsTableView, if that's what you want:
searchDC.searchResultsTableView.hidden=YES;
(searchDC is an instance of UISearchDisplayController)
This will help you with hiding the Cancel button and stuff: http://www.alexandre-gomes.com/?p=418
I've been trying to learn the new Storyboard feature in Xcode and I've run into a problem with trying to set a UITableView to edit mode.
So far my storyboard looks like this:
NavigationController -> UIViewController (subclass with tableview property)
I added a Navigation Item and a Bar Button item to the view controller scene, so I do see an edit button. It didn't do anything automagically, so I tried linking it's selector to the setEditing method of the tableview delegate. This did put it into editing mode. However, the edit button did not change to a "Done" button and so there is no way to get out of editing mode.
Do I have to create another Navigation item for the Done button? How do I connect it so that it appears at the right time and works correctly?
I think that also with Storyboard, the only way (for sure, the easiest one) to implement a working edit/done button, is to use the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
...
//set the edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
This is the solution that Apple itself implements if you select a "Master-Detail Application" template for your project.
Probably Storyboard is still not perfect, and hopefully it will be improved from Apple in next releases...
I just started using Storyboards, so I also wanted to use the Storyboard to add my Edit button. It is annoying to have taken the time to learn how to use a new tool but find you need a roll of duct tape to patch up the holes.
You can get it to work, but need to add a Custom button. In the Attributes inspector make sure the Identifier is Custom and the title is Edit.
Then add something like this in your .m
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = #"Edit";
[super setEditing:NO animated:YES];
} else {
sender.title = #"Done";
[super setEditing:YES animated:YES];
}
}
Have your Custom Edit button call the setEditMode method.
Can only hope they will fix the implementation of the Edit button in the Storyboard editor in the future.
To summarize:
The Button, returned by UIViewController.editButtonItem is a special toggling button with special behavior that calls - (void)setEditing:(BOOL)editing animated:(BOOL)animated if pressed.
The Button, returned by UINavigationController.editButtonItem is a simple Button, just labeled with "Edit".
The Storyboard allows to select the latter one.
If you are using the navigation controller to push to the view controller, simply set self.navigationItem.rightBarButtonItem = self.editButtonItem;, which will put the default Edit button in the right. If the navigation bar is not visible, call self.navigationController.navigationBarHidden = NO;. Those would be called in the viewDidLoad method, or something similar. Then in order to get the tableView to respond to the edit call, use the following method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
}
That should do what you want it to do. If you have any issues, just say so and we can narrow down the details
To add to #Graham answer, you might also want to change the style so you can have the "Done" button style (the blue color). Something like this:
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = #"Edit";
sender.style = UIBarButtonItemStylePlain;
[super setEditing:NO animated:YES];
} else {
sender.title = #"Done";
sender.style = UIBarButtonItemStyleDone;
[super setEditing:YES animated:YES];
}
}
one can use the dumb, not working Edit button from the Storyboard editor and then programmatically replace it with the UIViewController.editButtonItem.
in viewDidLoad:
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[toolbarItems replaceObjectAtIndex:0 withObject:self.editButtonItem];
[self setToolbarItems:toolbarItems];
this code assumes one has added the dumb Edit button as the leftmost item on the toolbar in the Storyboard.
In case that you have UIViewController and inside this you added a UITableVIew.
If you want to add an edit UIBarButton in order to interact with UITableView, try:
Add this line...
- (void)viewDidLoad
{
[super viewDidLoad];
...
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
}
and this method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myListTableView setEditing:editing animated:animated];
if(self.myListTableView.editing) {
NSLog(#"editMode on");
} else {
NSLog(#"editMode off");
}
}
where
#property (weak, nonatomic) IBOutlet UITableView *myListTableView;
I've implemented edit menu from my custom UITableViewCell class. I have a small problem of updating a table view from inside the custom table cell class. What is the best approach to do that?
TIA
Clarification: By edit menu I meant a standard Cut/Copy/Paste... menu, that can complies with a standard UIResponder protocol. I want to cut/copy/paste cells content, which resides in some data structure (kind of folders/files tree). The cell actually only reflects the data.
The menu shows up on tap & hold on table cell. The table is derived from UITableViewController and created on fly (not from the xib). Cut/Copy actions are allowed for folders & files, while Paste action is allowed only for folders. Actually I need to refresh only the folder cell, which shows the number of items inside.
So in my CustomCell in paste selector I do the following:
- (void)paste:(id)sender {
... Perform a paste of data...
MyTableViewController *myTable = (MyTableViewController *) delegate;
[myTable performSelector:#selector(updateData) withObject:nil afterDelay:0.3];
}
In MyTableViewController:
- (void) updateData
{
[self.tableView reloadData];
}
The thing is that all cells except of the one that was changed are redrawn. I see it in cellForRowAtIndex function. Even if I add in paste selector [self setNeedsDisplay] it doesn't help.
Also, my custom cell overrides setHighlighted function:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
if (delegate)
[delegate copyableTableViewCell:self willHighlight:highlighted];
[super setHighlighted:highlighted animated:animated];
}
so the delegate (MyTableViewController) shows an edit menu there.
And again the question is why the changed cell doesn't refresh?
Thanks
Do you want to update a single cell or the whole tableview? What about some kind of delegates, or selectors?
Resolved. Calling in MyTableViewController:
- (void) updateData
{
[self.tableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.3];
}
and it does the work...