my edit button can not change to done, delete row by swiping - iphone

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;
}

Related

How to remove the edit button on a Master-Detail Template

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.

exit edit mode and update nav bar items

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];

iphone, disable table sliding in edit mode

hi
I am editing table view in iphone. I m doing this with the following code written under viewDidLoad() method.
UIBarButtonItem* barBtnItemEdit = self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:#selector(turnOnEditing)];
My table view goes in edit mode correctly, but it slides a little to the right. Now what i want to do is to lock the table so that it remains on its position even in edit mode. I am writing customized code for table in editing mode instead of default Delete or insert behavior..
I've implemented the following code to stop displaying "red-color delete" button in front of each row...,
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
but UITableVIew still slides a little to the right. Now how to lock the table so that it doesn't change its position in edit mode
Best regards,
Abdul Qavi
I think you also need to add:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

Go to previous view if no items in UITableView

I have a Tab Bar Controller with UITableView inside. When an item is selected from the table, it pushes another View with a UITableView. What I want to achieve is when the second View has 0 rows (all rows deleted) to simulate the backBarButton behavior - to go to the previous view (the one with the Tab Bar Controller).
In -viewDidAppear:
if ([yourContentArray count] == 0) { // Or other way to check if there are 0 rows
[self.navigationController popViewControllerAnimated:YES];
}
If you want to go back once several rows have been deleted, you could put it into the Delegate, too:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if ([yourContentArray count] == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
// ...
}
}
Is your view Hierarchy like
UITabBarViewController
|--UINavigationController
|--UITableViewController1
|--UITableViewController2
If yes, then you should be able to popViewControllerAnimated: on the navigation controller.
Now I really, I mean really, don't advice to do that since it will create a very strange feeling for your user. If there is nothing to show, just don't let the user believe there is. Hide the mark for that row in the first tableView.

iphone : SDK disclosure button not response from first TableView to second detail TableView?

How can I pass the first tableview row data to second detail view row
I use
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
or
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
to show up disclosure button in each cells
it can ,I also add actions in
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(#"disclosure button %# touched",indexPath.row);
}
But when I touch the disclosure button I got nothing shows up in console
Than I add actions in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// carry Alarm Name text into sub-table-view to look for detail time and text info
NSLog(#"Alarm Name = %#", [alarmName objectAtIndex:indexPath.row]);
NSLog(#"Index Path Raw# = %i", indexPath.row);
// Navigation logic may go here. Create and push another view controller.
AlarmDetailTableViewController *detailViewController = [[AlarmDetailTableViewController alloc] initWithNibName:#"AlarmTableViewController" bundle:[NSBundle mainBundle]];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
detailViewController = nil;
}
It can notified on console mode
But I think it always appear the same detail view when I touch any rows in First Level TableView
How to show a new detail view with data from Level one ?
for example
To make the tableView1.row0.text = detail view.title
and show other data under the title
if you need the code I can upload it
my boss said it's not big secret inside...
Thanks a lot
Perhaps the problem is with your:
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
which should be reading:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
Ok,if you tapped on accessory button than show another detail view
we need to add a detail view
First create a "new navigation based application",ex FirstView
right click on the class folder and choice "add new File" choice a new view you wanna display
ex I selected a new tableview named "DetailView"
than first go to the DetailView.m go give a section and row numbers
or declare the layout
than back to FirstView.h
add #import DetailView
go to FirstView.m
find this method - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
and use DetailView sub class to create a new view
add the codes like this
DetailView *detailView =[[DetailView alloc]init];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:detailView animated:YES];
you can add any view you want to display