Bronze / Silver / Gold Challenge


#1

Bronze
This one is pretty simple, just add the following to BNRItemsViewController.m

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Remove";
}

Silver
Pretty easy as well. All these changes are in BNRItemsViewController.m


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Increase the count by one to account for no more items
    return [[[BNRItemStore sharedStore]allItems]count]+1;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger lastRow = [[[BNRItemStore sharedStore]allItems]count] +1;
    if (row == lastRow - 1) {
        return NO;
    } else {
        return YES;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    if (indexPath.row == [[[BNRItemStore sharedStore]allItems]count]) {
        // Add at the end no more items
        cell.textLabel.text = @"No more items...";
        return cell;
    }
    // Set the text on the cell with the description of the item
    // that is at the nth index of items, where n = row this cell
    // will appear in on the tableview
    NSArray *items = [[BNRItemStore sharedStore]allItems];
    BNRItem *item = items[indexPath.row];
    
    cell.textLabel.text = [item description];
    return cell;
}

Gold
A bit more difficult, the main thing I tried to deal with is stopping other items moving up the last one as well as it being able to be edited. All these changes are in BNRItemsViewController.m

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    if (destinationIndexPath.row == [[[BNRItemStore sharedStore]allItems]count])
    {
        return;
    } else {
        [[BNRItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
    }

}

This is the important bit, we need to ensure if you try and set the proposed index to be the bottom item that it fails and goes back to the source index path.

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if( sourceIndexPath.section != proposedDestinationIndexPath.section )
    {
        return sourceIndexPath;
    }
    else
    {
        if (proposedDestinationIndexPath.row == [[[BNRItemStore sharedStore]allItems]count]) {
            return sourceIndexPath;
        } else {
            return proposedDestinationIndexPath;
        }
    }
}

#2

Great solution!
Thanks for the help.

Thought these 2 pieces of code were kind of redundant.


#3

I had trouble getting that last row in for “No More Items!” turns out i put too much thought into it when the solution was so simple. thanks


#4

Hey,guy. Thank you for your golden solution. I figured out the silver one because it is the same as the last chapter.But I have a question about the judgement on indexPath.row and the array count.
My code is almost same as your solution. I only put the key part.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    if(indexPath.row == [[[BNRItemStore sharedStore] allItems] count])
    {
        cell.textLabel.text = @"No more items!";
        NSLog(@"the ROW number is %lx",(long)indexPath.row);
         NSLog(@"the count number is %lx",(long) [[[BNRItemStore sharedStore] allItems] count]);
    }else{
        
        //Set the text on the cell with the description of the item
        //that is at the nth index of items,where n = row this cell will appear in on the tableview
        NSArray *items = [[BNRItemStore sharedStore] allItems];
        BNRItem *item = items[indexPath.row];
        cell.textLabel.text = [item description];
         NSLog(@"the row number is %lx",(long)indexPath.row);
        NSLog(@"the count number is %lx",(long) [[[BNRItemStore sharedStore] allItems] count]);
       }
    //NSLog(@"row number %ld",(long)indexPath.row);
    return cell;
}

And below is the NSLog result when I click New button several times, you can notice that I use ROW to see when the “No more items!” show.

Here is my point. I do not understand why the row become 0 and 0 again and 1 ,2,3… In last chapter, we initialise the table view with a given number of rows and the row number is normal, just from 0 to 1,2,3 as I expect. But in this chapter, in the beginning we do not initialise the table view with a given number of rows but we still use the same judgement as last chapter, so this time I really do not know why the row number grows in this way. Thank you for your explain.