Gold challenge


#1

This was an easy one. If you try to drag it further, it bounces back to where it came from

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

#2

Is there a reason you cast the count as an int and detract 1? I did this:

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

return proposedDestinationIndexPath;

}
[/code]

Which works just fine.


#3

Hi code works fine but there is one exception. If you try to move last row with No more Items program will crash. Here is the solution

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

return proposedDestinationIndexPath;

}
[/code]

And final part golden challenge was that you want to make the last line undeletable (the line with IF is solution)

[code]-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//pokud table view zada o potvrzeni delete prikazu
if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.row != [[[BNRItemStore sharedStore]allItems]count]) { // podminka ifu druha cast je kvuli tomu aby se nedal smazat posledni radek s no moreitems
NSArray *items = [[BNRItemStore sharedStore]allItems];
BNRItem *item = items[indexPath.row];
[[BNRItemStore sharedStore]removeItem:item];

    //vyjdme radek
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

}[/code]

Sorry for comment in other language in my code :slight_smile: bb


#4

The code actually doesn’t crash because in the silver challenge I have this code

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    if (indexPath.row == items.count)
        return FALSE;
    else
        return TRUE;
    
}

In addition, as you point out the last row cannot me modified. It always is the last row, cannot be deleted or edited. So just turn off editing completely

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    if (indexPath.row == items.count)
        return FALSE;
    else
        return TRUE;
 }