【问题标题】:UITableViewCell expand and collapse animation not working when collapsingUITableViewCell 折叠时展开和折叠动画不起作用
【发布时间】:2013-07-28 15:57:59
【问题描述】:

我很难弄清楚为什么我的 UITableViewCell 在未选中时不会为 contentView 设置动画。

我编写了一些代码来展开和折叠单元格,但不知何故,当折叠单元格时,它会为高度设置动画,而不是 contentView。其实好像 contentView 被移除了。

作为记录,我在 contentView 中添加了一个 UILabel。展开时,UILabel 的框架会相应地调整大小,但在折叠时,UILabel 只是被解除或隐藏而没有动画。

有什么建议吗?

这里是一些示例代码:

/* 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];
}

【问题讨论】:

    标签: uikit core-animation uitableview


    【解决方案1】:

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

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


    【讨论】:

    • 酷我回家看看
    【解决方案2】:

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

    Collapse And Expand

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      • 2011-07-08
      • 2018-12-13
      • 2017-03-09
      • 2013-06-12
      • 2012-12-04
      相关资源
      最近更新 更多