I have a weird problem with the indexPath.section from the cellForRowAtIndexPath: method.
I have a grouped tableview with 4 sections and I’m trying to apply a custom UITableViewCell for section 3, but it’s not working.
When I try if(indexPath.section==0){...} it works (and for section==1 and section==2 as well) but it fails for section==3. (?)
I don’t know why, that makes no sense.. Did someone already had this (strange) problem?
When I try if(indexPath.row==0){...} it works for all the 4 sections.. so..?!
Here is my 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;
}
Problem solved !
I just added if(indexPath.row) and it works fine.
Finally you got this :
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;
}
}
Well, you’re never allocating a DirectionsTableViewCell inside your
if(cell == nil).In this section of your code:
You never allocate a cell of type
DirectionsTableViewCellfor it to be reused later. I also notice you have an ivar nameddirectionsCellof typeDirectionsTableViewCell. Unless you’re allocating and setting it up elsewhere,cell = directionsCellends up assigning a nil object to yourcellTry this code instead and see if it works: