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];
Related
I am creating Project in which i have FirstViewController. In FirstViewController i have putted UITableView, and displayed all the data in proper way. On myTableView i putted functionality to edit UITabelView (delete or insert row)
I have EditBtton (UIBarButtonItem) on UINavigationBar, when i tapped it then my UITableView goes to in edit mode.
- (void) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[self.tblView setEditing:NO animated:NO];
[self.navigationItem.rightBarButtonItem setTitle:#"Edit"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
[self myCustomMethod]; // custom method
}
else
{
[super setEditing:YES animated:YES];
[self.tblView setEditing:YES animated:YES];
[self.tblView reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Done"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
For Your Info : All of work is properly and fine.
I want to only know that how to go my UITableView in editing mode even i have not use UITableViewController such like
#interface FirstViewController : UIViewController // here i have UIViewController not UITableViewController
Then how it is possible when i write condition such i use like,
if(self.editing) then it check proper way, Generally it should be if(self.tableViewObj.editing)
NOTE : I have not any error But I want to only know, how it is possible?, how it work ? and which one is best for me if(self.editing) or if(self.tableViewObj.editing)
It's all about your UIBarButtonItem. It switches your controller to editing state.
As documentation says:
#property(nonatomic, getter=isEditing) BOOL editing
Discussion
If YES, the view controller currently allows editing; otherwise, NO.
If the view is editable and the associated navigation controller contains an edit-done button, then a Done button is displayed; otherwise, an Edit button is displayed. Clicking either button toggles the state of this property. Add an edit-done button by setting the custom left or right view of the navigation item to the value returned by the editButtonItem method. Set the editing property to the initial state of your view. Use the setEditing:animated: method as an action method to animate the transition of this state if the view is already displayed.
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.
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 have a tabbed bar main window with four tabs, one of which is a list. This list is empty at first but when I click a "+" I am presented with a modal view of available options like this
- (IBAction)addThing:(id)sender {
MJAvailableThingsViewController *controller = [self availableThingsViewController];
[self presentModalViewController:availableThingsViewController animated:YES];
[controller setEditing:YES animated:NO];
}
This works.
The list of things has a UITableViewCellAccessoryDetailDisclosureButton on each row. This was done with the accessoryTypeForRowWithIndexPath. This works as well.
When I click the little blue button in this view the delegate accessoryButtonTappedForRowWithIndexPath is called. This works.
However, in accessoryButtonTappedForRowWithIndexPath I want to present a detail view of the thing and this is where I encounter problems:
The delegate looks like this:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
{
NSLog(#"accessoryButtonTappedForRowWithIndexPath");
NSLog(#"Tapped row %d", [indexPath row]);
[[self navigationController] pushViewController:thingDetailViewController animated:YES];
The program enters this code but nothing happens, that is I get the NSLog output but no new window. I am very new at this so please forgive me if this is obvious; to me it is, however, not... :-)
Do you have a navigationController? It sounds like you have a TabController but not a NavigationController.
Check [self navigationController], is it set to anything?
Here's a similar question:
Tab Bar Application With Navigation Controller