Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My suggestion would be to not implement the Add functionality as a button in the navigation bar. I assume that you're dealing with a table view of items below, so one way of handling this user interaction is to display an "Add new item" option as the last entry in your table view. This could be programmatically faded in when the user taps on the Edit button in your navigation bar by implementing the following delegate method:</p> <pre><code>- (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView beginUpdates]; [self.tableView setEditing:editing animated:YES]; if (editing) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } [self.tableView endUpdates]; } </code></pre> <p>You then would need to make sure that the extra row is accounted for by increasing the count of rows using the following:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView.editing) return ([objects count] + 1); else return [objects count]; } </code></pre> <p>and then showing the green plus sign to its left, as opposed to the normal deletion editing style:</p> <pre><code>- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone; if (indexPath.row &gt;= [objects count]) return UITableViewCellEditingStyleInsert; else return UITableViewCellEditingStyleDelete; return UITableViewCellEditingStyleNone; } </code></pre> <p>Of course, you'll need to supply a name for it in your cellForRowAtIndexPath: implementation and handle its row selection, as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload