只是作为一个背景,这个问题是与此相关的一个我刚才发布:Trying to expand/collapse UITableViewCell from a UIButton on the custom cellUITableViewCell的展开/折叠动画(工程拓展的时候,却没有崩溃)
总之,我有几个自定义表格单元格的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");
}];
}
经调查,我发现的“其他”分支的if-else,该buttonRect已经在相同的Y位置原来的位置。我怀疑这就是为什么没有动画发生,我不知道。
任何帮助都会非常棒!
这个答案没有任何与被问的问题。 – Dalmazio 2014-07-25 02:37:27
你能看到答案被接受吗? – 2014-07-25 06:14:08
是的,那更令人困惑。 – Dalmazio 2014-07-25 21:36:46