1

I am having a difficult time figuring out why my UITableViewCell wont animate the contentView when unselected.

I have made some code that will expand and collapse a cell, but somehow when collapsing the cell, it animates the height, but not the contentView. Actually it seems that the contentView is removed.

For the record I have a UILabel added to the contentView. When expanding, the UILabel's frame will resize accordingly, but when collapsing the UILabel is just dismissed or hidden without animations.

Any advice?

Here is some sample code:

/* Expanding and collapsing code */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self cellIsSelected:indexPath])
    {
        //Item is clicked, unclick it
        [self.selectedIndexes removeObject:indexPath];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else
    {
        //Item is not clicked. Deselect others and select this
        [self.selectedIndexes removeAllObjects];
        [self.selectedIndexes addObject:indexPath];
    }

    [tableView beginUpdates];
    [tableView endUpdates];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self cellIsSelected:indexPath]) {
        return 150;
    }
    return [tableView rowHeight];
}
chrs
  • 5,906
  • 10
  • 43
  • 74

2 Answers2

0

Try this, hope it helps

   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if(add == NO)// i took bool to compare change it for your need 
      {
      [self.aTableVIew deselectRowAtIndexPath:indexPath animated:YES];
//you need do deletions witin this commit and begin

[self.aTableVIew beginUpdates];
//animations are within beginUpdates and endUpdates otherwise no animations
[array removeObjectAtIndex:indexPath.row];
[self.aTableVIew deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.aTableVIew endUpdates];
    add = YES;
}
else
{

//else in your case add objects to array and reload the table
    [array removeAllObjects];
    [self.aTableVIew reloadData];
    [self.aTableVIew beginUpdates];
 //compute where to insert
    [array addObject:[NSString stringWithFormat:@"%d",indexPath.row]];
    [self.aTableVIew insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
    [self.aTableVIew endUpdates];
    add = NO;
}  
}


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

Look At my demo I made. It is simply collapse and expand sample project by clicking action on section header.

Collapse And Expand

May this will help you.

Community
  • 1
  • 1
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90