如何翻转iOS UITableViewCell? [英] How can I flip an iOS UITableViewCell?
问题描述
我有一个包含许多单元格的tableview。每个单元格都有一个小信息i按钮,我希望用户能够通过从右侧翻转单元格来编辑该单元格中的信息。
I have a tableview with a number of cells. Each cell has a little info "i" button, which I would like the user to be able to click in order to edit the information in that cell, by "flipping" the cell from the right.
我为两个可能的单元格状态中的每一个都有一个单独的表格单元格XIB,而cellForRowAtIndexPath提供了正确的XIB(具有不同的单元格标识符),具体取决于单元格是应该在常规视图中还是详细/编辑视图。使用例如reloadRowsAtIndexPaths可以很好地反映按下i按钮时改变的状态。
I have a separate table cell XIB for each of the two possible cell states, and cellForRowAtIndexPath provides the correct XIB (with a different cell identifier) depending on whether the cell is supposed to be in the regular view or the detail/edit view. This works fine using, e.g., reloadRowsAtIndexPaths to reflect the changed state when the "i" button is pressed.
但我无法弄清楚如何设置翻转动画当按下单元格的i按钮时。翻转不是reloadRowsAtIndexPaths中withRowAnimation的选项之一。使用像UIView animateWithDuration这样的标准动画方法不起作用:reloadRowsAtIndexPaths只会导致相关单元格立即重新加载,忽略动画指令。我甚至试图使用drawViewHierarchyInRect来获取单元格的未来状态作为图像,但我无法让单元格快速地重绘自己以便被drawViewHierarchyInRect拾取(我不希望用户实际看到结果动画之前的动画。)
But I just can't figure out how to animate a flip when the cell's "i" button is pressed. Flipping isn't one of the options for "withRowAnimation" in reloadRowsAtIndexPaths. And using a standard animation approach like UIView animateWithDuration doesn't work: reloadRowsAtIndexPaths just causes the relevant cell to reload instantly, ignoring the animation instructions. I even tried to use drawViewHierarchyInRect to pick up the cell's future state as an image, but I couldn't get the cell to redraw itself quickly enough to be picked up by drawViewHierarchyInRect (and I don't want the user to actually see the result of the animation before the animation).
有没有人知道如何处理这个?
Does anyone have an idea of how to handle this?
EDIT
总而言之,我遇到的挑战是:除了reloadRowsAtIndexPaths之外,还有另一种方法可以将UITableView单元格内容与不同的UITableViewCell Xib交换(打破动画),而不会给新Xib的IBOutlets造成麻烦? (将它作为Nib加载到子视图中似乎打破了IBOutlets。)到目前为止,答案还没有解决这个问题。
To summarize, the challenge I'm having is: is there another way to swap UITableView cell contents with a different UITableViewCell Xib besides reloadRowsAtIndexPaths (which breaks animations), without causing trouble with the IBOutlets of the new Xib? (Loading it as a Nib into a subview seems to break the IBOutlets.) The answers so far haven't addressed this.
推荐答案
如果您使用两个子类 UITableViewCell
,那将会很复杂,而是使用单个子类 UITableViewCell
,然后执行翻转动画例如,
It will be complicated if u use two subclassed UITableViewCell
, instead use a single subclassed UITableViewCell
, and perform flip-animation for example,
让xib在其contentView中包含子类 UITableViewCell
添加两个视图
let xib contains the subclass UITableViewCell
in its contentView add two views
- 正常查看
- 翻转视图
在这个视图中添加你想要显示的内容,并确保单元格中的hav插座
例如我拍摄
in this view's add the contents what u want to display, and make sure both hav outlet in the cell for example that i took
在上面的两个视图中第一个 View-FlipView
及其内容标签和按钮,第二个也与第一个视图相同,顶视图应该是normalView
连接按钮对单元格的操作和你只需在自定义单元格中执行如下操作
in the above two view first one View-FlipView
and its contents label and button and second one is also same as first view, top view should be the normalView
connect the button actions to cell and u just perform like below in custom cell
//flip the view to flipView
- (IBAction)flipButtonAction:(UIButton *)sender
{
[UIView transitionWithView:self.contentView duration:0.6 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
[self.contentView insertSubview:_flipView aboveSubview:_normalView];
} completion:^(BOOL finished) {
}];
}
//flip the view back to normalView
- (IBAction)flipBackButtonAction:(id)sender
{
[UIView transitionWithView:self.contentView duration:0.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self.contentView insertSubview:_normalView aboveSubview:_flipView];
} completion:^(BOOL finished) {
}];
}
它看起来像下面的东西
任何问题只是评论... :)希望这有助于你.. :)
any problem just comment ... :) hope this helps u .. :)
这篇关于如何翻转iOS UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!