Details
First, you need to implement the numberOfRowsInSection:
method in your TableView's data source.
This method takes the row of an NSIndexPath (which is an integer) as an argument. NSIndexPath, when used in TableViews, has the properties section
and row
. All you need to do is get the value of the section
property and return the number of rows you'd like to appear in that section. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section)
{
//This would represent your first section, "Today..."
case 0: //implement logic to find how many data objects need to be represented as rows for this section
break;
//second section, "Tomorrow"
case 1: //logic
break;
//third section
case 2: //logic
break;
}
}
Now that you've accomplished that, it's time to implement cellForRowAtIndexPath:
. Unlike the previous method, this one takes a whole NSIndexPath as an argument, usually called indexPath
in the docs. Simply find the section of the path using the property accessor indexPath.section
, and the row within the section with indexPath.row
.
Important Note
Having said that, all this might be a bit easier if you rearrange the information currently contained in the ShowTimes array. If the array isn't sorted, then (for example) the logic in each case I showed for numberOfRowsInSection:
will require you to traverse the entire array and test each element to see if it belongs in the table section being requested. I would recommend splitting the ShowTimes array into three separate arrays (one for each section) before the table is displayed, so that all you need is a simple count
call to the appropriate array in each case. The implementation for cellForRowAtIndexPath:
would be simplified as well.
Summary
Think of it this way. Methods like numberOfSectionsInTableView:
and numberOfRowsInSection:
decide the layout of rows in your table long before a single cell is even loaded - before your data is even considered. As each cell is about to be displayed, cellForRowAtIndexPath:
decides what that cell will contain based on the NSIndexPath of the cell, or more specifically, based on the section
and row
properties of that IndexPath.
In short, the only thing linking a cell to its content is the IndexPath, and cellForRowAtIndexPath:
decides how the link is made.
For further reading, take a look at this page from the TableView Programming Guide. Specifically, the section entitled "Populating the Table View With Data".