iphoneuitableviewcustomizing

problem with UITableViewCell when customizing with an extended class


I have 2 classes for a UITableViewCell that show different data but they use the same table.

I'm trying to populate the table in 2 steps by using a bool var, but it seems that I get an exception because the cell gains the first class assignment and can't mutate...

basicaly I have to cell extended classes

@interface PatientsCellNewPager : UITableViewCell { } 
@interface DoctorsCellNewPager : UITableViewCell { } 

the code brakes when I'm at step 2 and trying to change the type of the cell and gives an exception at [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] ]; on the phase==2 if block..

this is because cell according to the debugger still has the type DoctorsCellNewPager from the first initialization....

How can I do it otherwise?? It has to be on the same page using the same instance of the class but using multiple layouts and defining the cell layout the spot ..would be a terrible option

here's my code

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";



    if (phase==2) { //patient


        PatientsCellNewPager *cell =(PatientsCellNewPager *)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[PatientsCellNewPager alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }







        PatientsSet *set = (PatientsSet *)[patients_set objectAtIndex:indexPath.row];       

        NSLog(@"logg %d",indexPath.row);    


        [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] ]; 




           return cell;

    }
    else if (phase==1) { //doctor


        DoctorsCellNewPager *cell =(DoctorsCellNewPager *)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[DoctorsCellNewPager alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }





            // Set up the cell

            DoctorsSet *set = (DoctorsSet *)[doctors_set objectAtIndex:indexPath.row];      


              [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] pass:YES ];  





        return cell;


    }


    return nil;

}

here's my error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DoctorsCellNewPager setData:cellid:]: unrecognized selector sent to instance 0x181690'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x323ef64f __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x36632c5d objc_exception_throw + 24
    2   CoreFoundation                      0x323f31bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
    3   CoreFoundation                      0x323f2649 ___forwarding___ + 508

Solution

  • That's a very common issue and many people came across that - Just recently I had to deal with that issue, too.

    Matt Gallagher posted a great tutorial on how to accomplish that on his blog:

    Heterogeneous cells in a UITableViewController


    Well, I just got an idea on how it could work..
    // THIS IS HIGHLY EXPERIMENTAL AND HAS NOT BEEN TESTED:

    static NSString *PatientsCellIdentifier = @"PatientsCellNewPager";
    static NSString *DoctorsCellIdentifier = @"DoctorsCellNewPager";
    
    UITableViewCell *cell;
    if(patient) {
        cell = [tableView dequeueReusableCellWithIdentifier:PatientsCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:PatientsCellIdentifier] autorelease];
        }
        // Setup cell for patient -> maybe you could use -configureCell:atIndexPath:
    } else if (doctor) {
        cell = [tableView dequeueReusableCellWithIdentifier:DoctorsCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:DoctorsCellIdentifier] autorelease];
        }
        // Setup cell for doctor -> maybe you could use -configureCell:atIndexPath:
    }
    
    return cell;
    

    Let me know if this brought you any further..

    HTH