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];
}
4

2 回答 2

0

试试这个,希望对你有帮助

   -(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;
}  
}


于 2013-07-29T05:31:09.187 回答
0

看看我做的演示。它只是通过单击部分标题上的操作来折叠和展开示例项目。

折叠和展开

愿这对你有所帮助。

于 2013-07-29T05:37:26.720 回答