My own Bronze,Sliver and Golden solution


#1

Here is my own [size=150]Bronze,Sliver and Golden solution[/size] (Only code segment)

[size=150] Bronze[/size]
In BNRItemsViewController.m

[code]- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

return @“Remove”;
}[/code]

[size=150]Silver[/size]
In BNRItemStore.m
Add a NSString object into privateItems,and always keep it in the bottom in the array.

[code]- (instancetype)initPrivate{

self = [super init];

if(self){

    _privateItems = [[NSMutableArray alloc] init];
    [_privateItems addObject:@"No more items..."];
}

return self;

}

  • (BNRItem *)createItem{

    BNRItem *item = [BNRItem randomItem];

    // Keep NSString object “No more items…” at the bottom of array
    [self.privateItems insertObject:item atIndex:[self.privateItems count]-1];

    return item;
    }

[/code]

[size=150]Golden[/size]
In In BNRItemsViewController.m

[code]- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

// Insure the last row can not be edited
if (indexPath.row == ([[[BNRItemStore sharedStroe] allItems] count] - 1)) {
    return NO;
}else{
    return YES;
}

}

// Move Object

  • (void)tableView:(UITableView *)tableView
    moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
    toIndexPath:(NSIndexPath *)destinationIndexPath{

    [[BNRItemStore sharedStroe] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];

    // Refresh Data
    [tableView reloadData];

}
[/code]

In BNRItemStore.m

[code]- (void)moveItemAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{

// Return when move the last row
if(fromIndex == toIndex || (toIndex == [self.privateItems count] - 1)){

    return;

}
BNRItem *item = self.privateItems[fromIndex];

[self.privateItems removeObjectAtIndex:fromIndex];

[self.privateItems insertObject:item atIndex:toIndex];

}[/code]