text
stringlengths
0
13.4k
text label to the item鈥檚 name.
To display cells in your table
1.
In the project navigator, select XYZToDoListViewController.m.
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved.
100
Tutorial: Add Data
Display the Data
2.
Find the tableView:cellForRowAtIndexPath: data source method. The template implementation
looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
The template performs several tasks. It creates a variable to hold the identifier for the cell, asks the table
view for a cell with that identifier, adds a comment about where code to configure the cell should go, and
then returns the cell.
To make this code work for your app, you鈥檒l need to change the identifier to the one you set in the
storyboard and then add code to configure the cell.
3.
Change the cell identifier to the one you set in the storyboard. To avoid typos, copy and paste from the
storyboard to the implementation file. The cell identifier line should now look like this:
static NSString *CellIdentifier = @"ListPrototypeCell";
4.
Just before the return statement, add the following lines of code:
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
Your tableView:cellForRowAtIndexPath: method should look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ListPrototypeCell";
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved.
101
Tutorial: Add Data
Mark Items as Completed
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
return cell;
}
Checkpoint: Run your app. The list of items you added in loadInitialData should show up as cells in your
table view.
Mark Items as Completed
A to-do list isn鈥檛 much good if you can never mark items as completed. Now, you鈥檒l add support for that. A
simple interface would be to have the completion state toggle when the user taps the cell and to display
completed items with a checkmark next to them. Fortunately, table views come with some built-in behavior
that you can take advantage of to implement this simple interface鈥攕pecifically, table views notify their delegate
when the user taps a cell. So the task is to write the code that will respond to the user tapping a to-do item in
the table.
Xcode already made XYZToDoListViewController the delegate of the table view when you configured it
in the storyboard. All you have to do is implement the tableView:didSelectRowAtIndexPath: delegate