Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Details</h2> <p>First, you need to implement the <code>numberOfRowsInSection:</code> 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 <code>section</code> and <code>row</code>. All you need to do is get the value of the <code>section</code> property and return the number of rows you'd like to appear in that section. For example:</p> <pre><code>- (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; } } </code></pre> <p>Now that you've accomplished that, it's time to implement <code>cellForRowAtIndexPath:</code>. Unlike the previous method, this one takes a whole NSIndexPath as an argument, usually called <code>indexPath</code> in the docs. Simply find the section of the path using the property accessor <code>indexPath.section</code>, and the row within the section with <code>indexPath.row</code>.</p> <h2>Important Note</h2> <p>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 <code>numberOfRowsInSection:</code> 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 <code>count</code> call to the appropriate array in each case. The implementation for <code>cellForRowAtIndexPath:</code> would be simplified as well.</p> <h2>Summary</h2> <p>Think of it this way. Methods like <code>numberOfSectionsInTableView:</code> and <code>numberOfRowsInSection:</code> decide the layout of rows in your table <em>long before</em> a single cell is even loaded - before your data is even considered. As each cell is about to be displayed, <code>cellForRowAtIndexPath:</code> decides what that cell will contain based on the NSIndexPath of the cell, or more specifically, based on the <code>section</code> and <code>row</code> properties of that IndexPath.<br> In short, the only thing linking a cell to its content is the IndexPath, and <code>cellForRowAtIndexPath:</code> decides how the link is made.</p> <p>For further reading, take a look at this page from the <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW5" rel="nofollow noreferrer">TableView Programming Guide</a>. Specifically, the section entitled "Populating the Table View With Data".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload