在不重新加载单元格(通过 reloadData
或通过重新加载特定单元格和部分)的情况下,是否有动画化 UITableViewCell subview 中的帧更改的最佳实践。
示例:我有一个简单的 UIView
作为自定义 UITableViewCell
的 subview ,其中 subview 占据总行宽的一定百分比。我想为这个 UIView
的宽度变化设置动画(而容器 View ,UITableViewCell 内容 View 保持不变),但不重新加载,因为重新加载动画选项不显示框架 subview 以我喜欢的方式平滑地改变大小。
我最初的解决方案是遍历可见单元格,并使用 UIView
动画 block 手动更改每一帧,例如
for (CustomCell *cell in [self.tableView visibleCells]])
{
CGRect newFrame = CGRectMake(0.0,0.0,10.0,0.0); // different frame width
[UIView animateWithDuration:0.5 animations:^{
cell.block.frame = updatedFrame; // where block is the custom subview
}];
}
虽然这似乎适用于 iOS7,但在 iOS6 上会导致一些奇怪且难以解决的图形问题;遍历所有单元格似乎有些矫枉过正。
关于执行此操作的最佳方法有什么建议吗?
最佳答案
您可以使用 NSNotificationCenter 发布通知以通知每个单元格更改该特定 View 的宽度。
创建单元格后,您可以将其注册到特定通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"Notification"
object:nil];
然后您可以在每个单元格中处理通知并进行所需的更改:
- (void) receiveNotification:(NSNotification *) notification
{
CGRect newFrame = CGRectMake(0.0,0.0,10.0,0.0); // different frame width
[UIView animateWithDuration:0.5 animations:^{
self.block.frame = updatedFrame; // where block is the custom subview
}];
}
当你想改变宽度时你只需要发布事件。
[[NSNotificationCenter defaultCenter]
postNotificationName:@"Notification"
object:self];
不要忘记重用每个单元格,当单元格被释放时,请确保取消注册该事件。
[[NSNotificationCenter defaultCenter] removeObserver:self];
关于ios - 在不重新加载的情况下为 UITableViewCell subview 中的帧更改设置动画的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016213/