Scroll to delete or edit button pressed on iPhone.

I set my viewController to use the edit button in the way Apple recommends:

self.navigationItem.rightBarButtonItem = self.editButtonItem; 

When the user clicks the edit button, this triggers the setEditing:animated: method. In this method, I add or remove a "new line" depending on the value of editing (i.e., a line that the user can click to add a new item).

When the user iterates over a cell (until it is in edit mode), it also calls setEditing:animated: which leads to the fact that my code incorrectly adds this "new line". It should show this new line only if the entire viewController is in edit mode (i.e. when you click the edit button).

How can I solve this problem?

+8
iphone uitableview
source share
5 answers

If you implement tableView:willBeginEditingRowAtIndexPath: and tableView:didEndEditingRowAtIndexPath: behavior of the method calls will change ...

It will no longer call setEditing:animated: you must handle all the logic for editing changes within the two above methods. As a side effect of this, self.editing will no longer be YES when called with a swipe gesture. This is because setEditing:animated: is responsible for setting self.editing = YES .

+11
source share

Use the UITableViewDelegate tableView:willBeginEditingRowAtIndexPath: method tableView:willBeginEditingRowAtIndexPath: From the documentation:

This method is called when the user swipes horizontally across the line; as a result, the table view sets its editing property to YES (thereby switching to edit mode) and displays the "Delete" button in the line identified by indexPath. In this "Scroll to Delete" mode, the insert, delete and reorder controls are not displayed in the table view. This method gives the delegate the ability to configure the user interface of the application in edit mode. When the table exits edit mode (for example, the user clicks the Delete button), the table view calls tableView:didEndEditingRowAtIndexPath:

In tableView:willBeginEditingRowAtIndexPath: check the box so that the editing mode is started by clicking to delete. Then, in setEditing:animated: check the box to see if the editing mode worked normally, or by scrolling, delete and perform some actions based on this check. Finally, tableView:didEndEditingRowAtIndexPath: flag in tableView:didEndEditingRowAtIndexPath: so that the default action is executed when the edit button is clicked.

+3
source share

I have no idea what your "new line" is about, but you can attach your edit button to a helper method that sets a flag before calling setEditing, and there you can check if the specified flag is set and behave accordingly. Then clear the flag at the end of the method.

0
source share

The meaning of u is to override the setEditing: animated: method. or even u can set a flag in this method, as Eiko suggested.

or other way

u implements a custom navigation bar. in this case, you need an image of the navigation bar and 2 buttons above it. add a target to this button and implement functionality as needed

0
source share

The table enters the editing mode when the user presses the edit button, and also when you swipe the cell. To distinguish between these two cases, you can override willBeginEditingRowAtIndexPath (do not forget to call super or edit mode will not be entered) and save the index path in the property. Override setEditing and you can check the property and add the insert line only if it is zero (that is, when the user does not swipe).

 @interface MasterViewController () @property (nonatomic, strong, nullable) NSIndexPath *tableViewEditingRowIndexPath; @end @implementation MasterViewController - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{ self.tableViewEditingRowIndexPath = indexPath; [super tableView:tableView willBeginEditingRowAtIndexPath:indexPath]; // calls setEditing:YES } -(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{ [super tableView:tableView didEndEditingRowAtIndexPath:indexPath]; // calls setEditing:NO self.tableViewEditingRowIndexPath = nil; } - (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; if(self.tableViewEditingRowIndexPath){ // we are swiping return; } if(editing){ // insert row for adding } else{ // remove row for adding } } 
0
source share

All Articles