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

kur*_*kun 6 iphone uitableview uianimation

作为背景,这个问题与我之前发布的这个问题有关:尝试从自定义单元格上的UIButton扩展/折叠UITableViewCell

总而言之,我有一个带有几个自定义表格单元格的UITableView.每个自定义UITableViewCell都有一个文本区域,后面是"查看更多"按钮.基本上,每个单元格最初将显示3行文本,但是当用户点击"查看更多"按钮时,单元格应扩展到整个文本区域高度.再次点击按钮将使单元格崩溃.

我认为这很难想象,所以我在这里拍了一段简短的视频:http://dl.dropbox.com/u/762437/cell-animation.mov

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

我有扩展/崩溃工作(无论我做错了是另一回事!),但动画并不像我期望的那样工作.扩展动画有效,但不是崩溃.

在每个自定义UITableViewCell类中,当用户点击"查看更多"按钮时,我会调用一个IBAction.我还跟踪当前在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");
                             }];
        }
Run Code Online (Sandbox Code Playgroud)

在调查时,我发现在if-else的"else"分支中,buttonRect已经与原始位置处于相同的Y位置.我怀疑这就是没有动画发生的原因,我不确定.

任何帮助都会非常棒!

Nir*_*iya -2

简单的方法来做到这一点......

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

insertIndexPaths 是要插入到表中的 NSIndexPaths 数组。

deleteIndexPaths 是要从表中删除的 NSIndexPaths 数组。

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

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];
Run Code Online (Sandbox Code Playgroud)

从这个问题得到的...这个问题...

  • 是的,这更令人困惑。 (3认同)
  • 这个答案与提出的问题无关。 (2认同)