text
stringlengths 0
13.4k
|
---|
method to respond to user taps and update your to-do list items appropriately. |
When a cell gets selected, the table view calls the tableView:didSelectRowAtIndexPath: delegate |
method to see how it should handle the selection. In this method, you鈥檒l write code to update the to-do item鈥檚 |
completion state. |
To mark an item as completed or uncompleted |
1. |
In the project navigator, select XYZToDoListViewController.m. |
2. Add the following lines to the end of the file, just above the @end line: |
#pragma mark - Table view delegate |
- (void)tableView:(UITableView *)tableView |
didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
{ |
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved. |
102 |
Tutorial: Add Data |
Mark Items as Completed |
} |
Try typing the second line instead of just copying and pasting. You鈥檒l find that code completion is one of |
the great time-saving features of Xcode. When Xcode brings up the list of potential completions, scroll |
through the list until you find the one you want and then press Return. Xcode inserts the whole line for |
you. |
3. |
You want to respond to taps but not actually leave the cell selected. Add the following code to deselect |
the cell immediately after selection: |
[tableView deselectRowAtIndexPath:indexPath animated:NO]; |
4. |
Search for the corresponding XYZToDoItem in your toDoItems array. |
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row]; |
5. |
Toggle the completion state of the tapped item. |
tappedItem.completed = !tappedItem.completed; |
6. |
Tell the table view to reload the row whose data you just updated. |
[tableView reloadRowsAtIndexPaths:@[indexPath] |
withRowAnimation:UITableViewRowAnimationNone]; |
Your tableView:didSelectRowAtIndexPath: method should look like this: |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath |
*)indexPath |
{ |
[tableView deselectRowAtIndexPath:indexPath animated:NO]; |
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row]; |
tappedItem.completed = !tappedItem.completed; |
[tableView reloadRowsAtIndexPaths:@[indexPath] |
withRowAnimation:UITableViewRowAnimationNone]; |
} |
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved. |
103 |
Tutorial: Add Data |
Mark Items as Completed |
Checkpoint: Run your app. The list of items you added in loadInitialData is visible as cells in your table |
view. But when you tap items, nothing seems to happen. Why not? |
The reason is that you haven鈥檛 configured the table view cell to display the completion state of an item. To do |
so, you need to go back to the tableView:cellForRowAtIndexPath: method and configure the cell to |
display an indicator when an item is completed. |
One way to indicate that an item is completed is to put a checkmark next to it. Luckily, table view cells can |
have a cell accessory on the right side. By default, there鈥檚 no accessory; however, you can change the cell to |
display a different accessory, one of which is a checkmark. All you have to do is set the cell accessory based |
on the completion state of the to-do item. |
To display an item鈥檚 completion state |