Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird behavior of indexPath.section in "cellForRowAtIndexPath:"
    primarykey
    data
    text
    <p>I have a weird problem with the <code>indexPath.section</code> from the <code>cellForRowAtIndexPath:</code> method. </p> <p>I have a grouped tableview with 4 sections and I'm trying to apply a custom <code>UITableViewCell</code> for section 3, but it's not working. </p> <p>When I try <code>if(indexPath.section==0){...}</code> it works (and for <code>section==1</code> and <code>section==2</code> as well) but it fails for <code>section==3</code>. (?) </p> <p>I don't know why, that makes no sense.. Did someone already had this (strange) problem? </p> <p>When I try <code>if(indexPath.row==0){...}</code> it works for all the 4 sections.. so..?!</p> <p>Here is my code : </p> <pre><code>//ViewController.h import "DirectionsTableViewCell.h" DirectionsTableViewCell *directionsCell; // customized UITableViewCell //ViewController.m if (indexPath.section==3) { static NSString *CellIdentifier = @"directionsCell"; DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil]; cell = directionsCell; } return cell; } else { static NSString *CellIdentifier = @"defaultCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = @"Test"; return cell; } </code></pre> <p><br/></p> <p><strong>Problem solved !</strong></p> <p>I just added <code>if(indexPath.row)</code> and it works fine.</p> <p>Finally you got this :</p> <pre><code>if(indexPath.section==3) { if(indexPath.row) { static NSString *CellIdentifier = @"directionsCell"; DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil]; cell = directionsCell; } return cell; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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