Jim
Jim

Reputation: 9234

iPhone: UITableView sections

I have a table view with sections.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return numberHigherThen0;
}

in cellForRowAtIndexPath method I setup my rows according to section

- (UITableViewCell *)tableView:(UITableView *)aTableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   if (0 == indexPath.section) {
       //setup rows
   } esle {
     //setup rows
   }
}

numbers of sections and rows in sections changes dynamically. But numbers of sections always > 0. So in cellForRowAtIndexPath indexPath.section == 0 there will be always this case, right ? But some times cellForRowAtIndexPath didn't pass case indexPath.section == 0 !!! Why ? When it can be true ? When cellForRowAtIndexPath can start count sections from 1 ?

Update: section 2 has no rows

Upvotes: 1

Views: 227

Answers (2)

Thomas Deniau
Thomas Deniau

Reputation: 2573

tableView:cellForRowAtIndexPath: is only called when the table view needs to refresh a cell at a given index path.

So if section 0 is visible when your app starts, yes, you should get a call with section == 0 at least once. In subsequent refreshes, section 0 might be skipped if UITableView thinks it doesn't need to refresh it. This might be what you're seeing.

Use reloadRowsAtIndexPaths:withRowAnimation:, or reloadSections:withRowAnimation:, etc. to tell the UITableView that a row has changed and that it needs to refresh it.

Upvotes: 0

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

In numberOfSectionsInTableView: you specify the number of sections - however, the sections are numbered from 0.

So if you return 3 in your numberOfSectionsInTableView you will have three sections - section 0, section 1, and section 2.

Upvotes: 1

Related Questions