Q:

Deleting tableview row and section if empty with commitEdittingStyle

I can't seem to find too much on this. I'm currently trying to create a swipable delete button that will delete the row that is swiped, and if that row is now empty from the section header it will delete the section header as well. For example, "Bread" is swiped to delete, and there is nothing else under the section header "B". Then this will delete both Bread, and the "B" section header. My code is below. 
@interface ChoicesTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *items;
@property (strong, nonatomic) NSMutableDictionary *alphabetizedItems;

@end

@implementation ChoicesTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    self.items = [[NSMutableArray alloc] init];
    [self.items addObject:@"Apples"];
    [self.items addObject:@"Bread"];
    self.alphabetizedItems = [self alphabetizeItems:self.items];
}

//Segue if the item is tapped
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    MyDataChoices *currentRow = self.arrayNames[indexPath.row];
//    self.mySelectedCell = currentRow.myNameChoices;
//    
//    [self performSegueWithIdentifier:@"unwindSegueAction" sender:self];
//    
//}
////unwind segue from add choice
- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue
{

    AddChoiceViewController *sourceVC = segue.sourceViewController;
    NSString *myNewItem = sourceVC.myTextField.text;
    //NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString];
    NSString *stringCapitalized = [myNewItem capitalizedString];
    [self.items addObject:stringCapitalized];
    self.alphabetizedItems = [self alphabetizeItems:self.items];
    //[self.arrayNames addObjectsFromArray:@[[MyDataChoices itemWithNewName:stringCapitalized]]];
    [self.tableView reloadData];

}
//titles for talble view
#pragma mark Helper Methods
- (NSMutableDictionary *)alphabetizeItems:(NSArray *)items {
    NSMutableDictionary *buffer = [[NSMutableDictionary alloc] init];

    // Put Fruits in Sections
    for (int i = 0; i < [items count]; i++) {
        NSString *fruit = [items objectAtIndex:i];
        NSString *firstLetter = [[fruit substringToIndex:1] uppercaseString];

        if ([buffer objectForKey:firstLetter]) {
            [(NSMutableArray *)[buffer objectForKey:firstLetter] addObject:fruit];

        } else {
            NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:fruit, nil];
            [buffer setObject:mutableArray forKey:firstLetter];
        }
    }

    // Sort Fruits
    NSArray *keys = [buffer allKeys];
    for (int j = 0; j < [keys count]; j++) {
        NSString *key = [keys objectAtIndex:j];
        [(NSMutableArray *)[buffer objectForKey:key] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:buffer];
    return result;
}
#pragma mark title indexing

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *keys = [[self.alphabetizedItems allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [keys objectAtIndex:section];
    return key;
}

# pragma mark main table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView
{
    NSArray *keys = [self.alphabetizedItems allKeys];
    return [keys count];
}

-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
    //return self.arrayNames.count;
    NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:section];
    NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
    return [fruitsForSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //MyDataChoices *currentRow = self.arrayNames[indexPath.row];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath];

    //cell.textLabel.text = currentRow.myNameChoices;
    NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
    NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
    NSString *fruit = [fruitsForSection objectAtIndex:[indexPath row]];

    [cell.textLabel setText:fruit];

    return cell;

}

# pragma Mark delete slide button

//Delete Swipe Button
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        int index = indexPath.row;
        //[self.items removeObjectAtIndex:index];
        [self.alphabetizedItems removeObjectForKey:indexPath];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

A:

The basic approach is to see how many rows are in the section for the row being deleted. If the section has two or more rows, simply delete the row as you are doing now. If the section only has one row (the one being deleted), then remove the section from the data model and then delete the section from the table instead of deleting the row.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
        NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
        NSArray *fruitsForSection = [self.alphabetizedItems objectForKey:key];
        if (fruitsForSection.count == 1) {
            // Delete the whole section
            [self.alphabetizedItems removeObjectForKey:key];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        } else {
            // Delete the row from the data source
            NSInteger index = indexPath.row;
            //[self.items removeObjectAtIndex:index];
            [self.alphabetizedItems removeObjectForKey:indexPath];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

