Lee Armstrong
Lee Armstrong

Reputation: 11452

Custom Cell from Nib UITableView

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;

}

Upvotes: 1

Views: 12782

Answers (1)

Jeff Kelley
Jeff Kelley

Reputation: 19071

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:.

Upvotes: 7

Related Questions