text
stringlengths
0
13.4k
class. The declaration should look like this:
@interface XYZToDoListViewController ()
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
95
Tutorial: Add Data
Load the Data
@property NSMutableArray *toDoItems;
@end
3. Allocate and initialize the toDoItems array in the viewDidLoad method:
- (void)viewDidLoad
{
}
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
The actual code for viewDidLoad includes some additional lines—inserted by Xcode when it created
XYZListViewController—that are commented out. Feel free to leave them in.
At this point, you have an array that you can add items to. You’ll do this in a separate method,
loadInitialData, which you’ll call from viewDidLoad. This code goes in its own method because it’s a
modular task, and you can improve code readability by making this method separate. In a real app this method
would load the data from some sort of persistent store, such as a file. For now, the goal is to see how a table
view works with custom data items, so you’ll create some test data to experiment with.
Create an item in the way you created the array: Allocate and initialize. Then, give the item a name. This is the
name that will be shown in the table view. Do this for a couple of items.
To load initial data
1.
Add a new method, loadInitialData, below the @implementation line.
- (void)loadInitialData {
}
2.
In this method, create a few list items, and add them to the array.
- (void)loadInitialData {
XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
item1.itemName = @"Buy milk";
[self.toDoItems addObject:item1];
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
96
Tutorial: Add Data
Load the Data
XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
item2.itemName = @"Buy eggs";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
item3.itemName = @"Read a book";
[self.toDoItems addObject:item3];
}
3.
Call the loadInitialData in the viewDidLoad method.
- (void)viewDidLoad
{
}
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
Checkpoint: Build your project by choosing Product > Build. You should see numerous errors for the lines of
your loadInitialData method. The key to what’s gone wrong is the first line, which should say “Use of