Silver Challenge Question


#1

Hi,
I completed the silver challenge by looking at someone else’s solution on the forum, but I am confused.

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row]==[[[BNRItemStore defaultStore]allItems]count]) { return NO; } else { return YES; } }

I am not really sure why this works. I am confused about the condition statement. Can anyone help explain what the condition is saying? Thanks for the help.


#2

The view sends the message tableView:canMoveRowAtIndexPath: to the dataSource (in the application that’s ItemViewController) asking if it can move the row at a particular indexPath. The controller sends a BOOL back to tell the view if it should allow the movement of the row at the indexPath.

In this implementation, if the indexPath row property is equal to the number of objects in allItems then the answer is NO. I’m assuming “defaultStore” is similar if not the same as the book’s “sharedStore,” and that the author of this code has ensured the last object in allItems will always be an object that stores @“No More Items.” In short, this implementation means you can’t rearrange or move the last object in the allItems array and subsequently the last row of the tableView.

I hope this helps. If not, I’ll be happy to try and answer another question. Also, check out the documentation for tableView:canMoveRowAtIndexPath:. It may be helpful. Here’s the link to Apple’s “Table View Programming Guide for iOS:”

https://developer.apple.com/library/ios/documentation/userexperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html