More than 1 rightBarButtonItem on navigation bar

前端 未结 9 1134
清歌不尽
清歌不尽 2021-01-30 02:06

I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.

Obviously I can\'t make it using Interface Builder.

Does anyb

9条回答
  •  野性不改
    2021-01-30 02:32

    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:

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

    You then would need to make sure that the extra row is accounted for by increasing the count of rows using the following:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        if (tableView.editing)
            return ([objects count] + 1);
        else
            return [objects count];     
    }
    

    and then showing the green plus sign to its left, as opposed to the normal deletion editing style:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
        if (indexPath.row >= [objects count]) 
            return UITableViewCellEditingStyleInsert;
        else
            return UITableViewCellEditingStyleDelete;
    
        return UITableViewCellEditingStyleNone;
    }
    

    Of course, you'll need to supply a name for it in your cellForRowAtIndexPath: implementation and handle its row selection, as well.

提交回复
热议问题