Custom UITableViewCell is created twice ... Driving me crazy

I have created a custom UITableViewCell that I want to dynamically expand and contract when a button clicks on a cell. The method is called when the button is pressed, but it will seem like the cell is being created twice ... So resetting the state ... I've poured this many times over the course of several days trying different things, but I don't get what's wrong ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CategoryCell";

// Create a new Cell if necessary

CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

if (cell == nil) {
    cell = [[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.frame = CGRectMake(0.0, 0.0, 320.0, 67.0);

    NSLog( @"Cell Creation - Row %d", indexPath.row );

    [cell.expandContractButton addTarget:self action:@selector(expandContractButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
else {
    NSLog( @"Cell Found - Row %d", indexPath.row );
}

// Setup the cell ...

      

Log ....

2012-03-24 11:44:02.158 Review Writer[13523:fb03] Number of rows 1
2012-03-24 11:44:02.172 Review Writer[13523:fb03] Cell Creation - Row 0
2012-03-24 11:44:02.192 Review Writer[13523:fb03] Row 0 - setting height
2012-03-24 11:44:02.197 Review Writer[13523:fb03] Cell Creation - Row 0

      

Any guidance as to why this would be the case?

Code to fill cell data

-(void)tableView:(UITableView *)tableView willDisplayCell:(CategoryCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Setting up cell on Row %d - Expanded - %@", indexPath.row, cell.expanded ? @"YES" : @"NO");

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Category *cat = [categoryList objectAtIndex:indexPath.row];

cell.categoryLabel.text = cat.category;
cell.ratingLabel.text = cat.overall_rating;

if (cell.expanded == YES)
{
    [cell expandCell];
}
else {
    [cell collapseCell];
}

cell.reviewText.text = cat.review_text;

if([cat.overall_rating isEqualToString:@"Not Rated"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"0_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Unsatisfactory"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"1_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Needs improvement"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"2_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Meets job requirements"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"3_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Exceeds job requirements"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"4_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Outstanding"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"5_stars.png"]];
}    

// Put the accessory disclosure button on the cell

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}

      

Code for setting cell height ...

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
// Use the method that you would have created above to get the cell.

CategoryCell *cell = (CategoryCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

NSString *text = cell.reviewText.text;

// Note the 30 is a fudge factor... :-) Otherwise it does not include the last line...

CGFloat width = [cell.reviewText frame].size.width - 30;

CGFloat height = [text sizeWithFont:cell.reviewText.font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap].height;

NSLog( @"Row %d - setting height - Expanded = %@", indexPath.row, cell.expanded ? @"YES" : @"NO" );

if (cell.expanded == NO)
{
    height = 0;
}

return height + 67; 
}

      

Code to expand / collapse a cell ...

- (IBAction) expandContractButtonPressed:(id) sender{

CategoryCell *clickedCell = (CategoryCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];

CategoryCell *cell = (CategoryCell*)[self tableView:self.tableView cellForRowAtIndexPath:clickedButtonPath];

NSLog( @"EXPAND / COLLAPSE Row %d Exanded = %@", clickedButtonPath.row, clickedCell.expanded ? @"YES" : @"NO");

if (clickedCell.expanded == YES)
{
    NSLog( @"Collapse" );
    [cell collapseCell];
    clickedCell.expanded = NO;
}
else {
    NSLog( @"Expand" );
    [cell expandCell];
    clickedCell.expanded = YES;
}

NSLog( @"Expanded after = %@", clickedCell.expanded ? @"YES" : @"NO" );

NSArray *indexPaths = [[NSArray alloc] initWithObjects:clickedButtonPath, nil];

[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
}

      

+3


source to share


2 answers


Just a guess, since you only provided small portions of your code: you may have multiple sections. You are only checking the line, not the section, so the code can be called for each section.



0


source


However, your code should still handle this situation (multiple creation) and display the cell correctly.

You didn't give us the code to set the height and content of the cell.



You should be ok if you only save the cell creation code to cellForRowAtIndexPath:

and move all the cell setup code to willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

.

0


source







All Articles