text
stringlengths 0
13.4k
|
---|
numberOfRowsInSection:(NSInteger)section |
{ |
#warning Incomplete method implementation. |
// Return the number of rows in the section. |
return 0; |
} |
You want to return the number of list items you have. Fortunately, NSArray has a handy method called |
count that returns the number of items in the array, so the number of rows is [self.toDoItems count]. |
3. |
Change the tableView:numberOfRowsInSection: data source method to return the appropriate |
number of rows. |
- (NSInteger)tableView:(UITableView *)tableView |
numberOfRowsInSection:(NSInteger)section |
{ |
} |
// Return the number of rows in the section. |
return [self.toDoItems count]; |
The last method, tableView:cellForRowAtIndexPath:, asks for a cell to display for a given row. Up until |
now, you鈥檝e been working with code only, but the cell to display for a row is very much part of your interface. |
Fortunately, Xcode makes it easy to design custom cells in Interface Builder. The first task is to design your cell |
and to tell the table view that instead of using static content, it鈥檚 going to be using prototype cells with dynamic |
content. |
To configure your table view |
1. Open your storyboard. |
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved. |
99 |
Tutorial: Add Data |
Display the Data |
2. |
Select the table view in the outline. |
3. With the table view selected, open the Attributes inspector |
in the utility area. |
4. |
In the Attributes inspector, change the table view鈥檚 Content attribute from Static Cells to Dynamic |
Prototypes. |
Interface Builder takes the static cells you configured and converts them all into prototypes. Prototype cells, |
as the name implies, are cells that are configured with text styles, colors, images, or other attributes as you |
want them to be displayed but that get their data from the data source at runtime. The data source loads a |
prototype cell for each row and then configures that cell to display the data for the row. |
To load the correct cell, the data source needs to know what it鈥檚 called, and that name must also be configured |
in the storyboard. |
While you鈥檙e setting the prototype cell name, you鈥檒l also configure another property鈥攖he cell selection style, |
which determines a cell鈥檚 appearance when a user taps it. Set the cell selection style to None so that the cell |
won鈥檛 be highlighted when a user taps it. This is the behavior you want your cells to have when a user taps an |
item in the to-do list to mark it as completed or uncompleted鈥攁 feature you鈥檒l implement later in this tutorial. |
To configure the prototype cell |
1. |
2. |
3. |
Select the first table view cell in your table. |
In the Attributes inspector, locate the Identifier field and type ListPrototypeCell. |
In the Attributes inspector, locate the Selection field and choose None. |
You could also change the font or other attributes of the prototype cell. The basic configuration is easy to work |
with, so you鈥檒l keep that. |
The next step is to teach your data source how to configure the cell for a given row by implementing |
tableView:cellForRowAtIndexPath:. This data source method is called by the table view when it wants |
to display a given row. For table views with a small number of rows, all rows may be onscreen at once, so this |
method gets called for each row in your table. But table views with a large number of rows display only a small |
fraction of their total items at a given time. It鈥檚 most efficient for table views to only ask for the cell for rows |
that are being displayed, and that鈥檚 what tableView:cellForRowAtIndexPath: allows the table view to |
do. |
For any given row in the table, fetch the corresponding entry in the toDoItems array and then set the cell鈥檚 |