Iphone UItableViewCell展开/折叠动画(展开时工作,但不折叠)

Iphone UItableViewCell展开/折叠动画(展开时工作,但不折叠),iphone,uitableview,uianimation,Iphone,Uitableview,Uianimation,作为背景,这个问题与我之前发布的这个问题有关: 总之,我有一个UITableView,其中包含几个自定义表格单元格。每个自定义UITableViewCell都有一个文本区域,后跟一个“查看更多”按钮。基本上,每个单元格最初将显示3行文本,但当用户点击“查看更多”按钮时,单元格应扩展到整个文本区域高度。再次点击按钮将使电池崩溃 我认为这很难想象,因此我在这里拍摄了一段短片: (它大约是一个3.5mb的文件,所以加载不应该花那么长时间)。在视频中,我显示了一个单元格正在执行展开/折叠动画。展开时,“

作为背景,这个问题与我之前发布的这个问题有关:

总之,我有一个UITableView,其中包含几个自定义表格单元格。每个自定义UITableViewCell都有一个文本区域,后跟一个“查看更多”按钮。基本上,每个单元格最初将显示3行文本,但当用户点击“查看更多”按钮时,单元格应扩展到整个文本区域高度。再次点击按钮将使电池崩溃

我认为这很难想象,因此我在这里拍摄了一段短片:

(它大约是一个3.5mb的文件,所以加载不应该花那么长时间)。在视频中,我显示了一个单元格正在执行展开/折叠动画。展开时,“查看更多”按钮向下设置动画。当再次点击它时,它只是跳回到其原始位置,没有动画编辑:对不起,我应该更清楚一些。UITableView正在正确设置单元格的动画,我要问的是如何使“查看更多”按钮正确设置动画。

我已经让展开/折叠工作(我是否做对了是另一回事!),但是动画没有像我预期的那样工作。展开的动画有效,但塌陷的动画无效

在每个自定义UITableViewCell类中,我都有一个iAction,当用户点击“查看更多”按钮时会调用它。我还跟踪当前在NSMutableArray中扩展的单元。当用户点击“关闭”按钮时,该单元格将从阵列中移除

在tableView的cellForRowAtIndexPath中,我检查数组以查看哪些单元格应展开,哪些单元格应以默认大小显示

我使用以下代码执行此操作:

// Check if the array contains the particular cell, if so, then it needs to be expanded 
if([expandedRows containsObject:indexPath])
        {
            // Expand the cell's text area to fit the entire contents
            [cell.textLabel sizeToFitFixedWidth:cell.textLabel.frame.size.width];

            // Find the new Y-position of where the "Close" button.
            // The new Y position should be at the bottom of the newly expanded text label

            CGFloat bottomYPos = cell.textLabel.frame.origin.y + cell.textLabel.frame.size.height;

            // Get a rect with the "Close" button's frame and new Y position
            CGRect buttonRect = cell.showAllButton.frame;
            buttonRect.origin.y = bottomYPos;

            // Animation block to shift the "Close" button down to its new rect             
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveLinear
                             animations:^{
                                 cell.showAllButton.frame = buttonRect;
                                 [cell.showAllButton setTitle:@"Close" forState:UIControlStateNormal];
                             } 
                             completion:^(BOOL finished){
                                 NSLog(@"Done 1!");
                             }];
        }
        else
        {
            // This cell is currently collapsed

            // Get the button rect and subtract the height delta to put it back
            // OriginalCellSizes is where I keep track of the original Y positions
            CGRect buttonRect = cell.showAllButton.frame;
            buttonRect.origin.y -= cell.frame.size.height - [[originalCellSizes objectAtIndex:indexPath.row] floatValue];

            // Animation block for the Button    
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveEaseOut
                             animations:^{
                                 cell.showAllButton.frame = buttonRect;
                                 [cell.showAllButton setTitle:@"View More" forState:UIControlStateNormal];
                             } 
                             completion:^(BOOL finished){
                                 NSLog(@"Done! 2");
                             }];
        }
经过调查,我发现在if-else的“else”分支中,buttonRect已经处于与原始位置相同的Y位置。我怀疑这就是为什么没有动画发生的原因,但我不确定


任何帮助都会非常好

这样做的简单方法

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
InsertIndExpath是要插入到表中的NSIndExpath数组

DeleteIndExpath是要从表中删除的NSIndExpath数组

索引路径的数组格式示例:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];

从这个问题中得到答案…

这个答案与所问的问题无关。是的,这更令人困惑。