I cant seem to wrap my head around this. I have googled, and overstacked for hours now looking for examples that i can relate to. What I have is two arrays.
The name of my first NSMutableArray is "showDates".
I have 3 objects in here.
Object 0: "Today, May 20th"
Object 1: "Tomorrow, May 21st"
Object 2: "Saturday, May 22nd"
Then I have my second NSMutableArray named "showTimes" I have about 15 objects in there with开发者_如何转开发 strings in each object. ( i hope that makes sense? ) Each object is structured like this:
Object 0:
showID @"98022"
eventID @"833"
showTime @"1:30pm"
showDate @"Today, May 20th"
auditorium @"9"
venue @"2991"
Object 1:
showID @"98222"
eventID @"813"
showTime @"2:30pm"
showDate @"Tomorrow, May 21st"
auditorium @"9"
venue @"2991"
Etc, etc, ....
I have the headers working great in my tableView, but I cant seem to figure out how to add the objects in my "showTimes" array under the correct header. Any help would be greatly appreciated.
In your table view's dataSource class, you need to implement the method tableView:cellForRowAtIndexPath: to provide the cell for each row in the table. Using
[indexPath section]
where indexPath is the second parameter passed into the method, you can determine which section the cell that was requested belongs to (you need to also implement numberOfRowsInSection: to inform the table view of how many cells it should request in each section). Hope that helps.
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".
精彩评论