Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I flip an iOS UITableViewCell?

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.

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.

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

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.

like image 398
Dan Avatar asked Nov 29 '22 00:11

Dan


1 Answers

It will be complicated if u use two subclassed UITableViewCell, instead use a single subclassed UITableViewCell, and perform flip-animation for example,

let xib contains the subclass UITableViewCell in its contentView add two views

  1. normal View
  2. Flipped View

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

enter image description here

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) {

   }];
  }  

and it will look like something below

enter image description here

any problem just comment ... :) hope this helps u .. :)

like image 146
Shankar BS Avatar answered Dec 04 '22 06:12

Shankar BS