views:

2723

answers:

1

Hello,

I have been trying various methods with no success to alter the following code...

I have a nib file that works fine if I set all cells to this but I only want one of the switch statements to have the custom cell nib file. Any ideas?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    UITableViewCell *cell;


    switch (section) 
    {
     case 0:

      cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
      cell.text =  [showTimes objectAtIndex:row];
      break;
     case 1:

      cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
      cell.text =  [showTimes objectAtIndex:row];
      break;
     default:
      cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
      cell.text =  [showTimes objectAtIndex:row];
      break;
    }




    return cell;

}
+2  A: 

You're going to need to do two things:

  1. Make a separate cell identifier for the cell that you want to use from the nib. Using "any-cell" will result in your cell being reused for different rows.
  2. Instead of UITableViewCell's initWithFrame:reuseIdentifier: method, you need to initialize the cell from your nib. Check Apple's Table View Programming Guide's section on using nibs.

Also, though this isn't technically required, UITableViewCell's initWithFrame:reuseIdentifier: method has been deprecated in favor of initWithStyle:reuseIdentifier:.

Jeff Kelley
Good article, thanks for the help!
Lee Armstrong