UItableViewCell Expand / collapse animation (works on expansion, but does not crash)

as a background, this question is related to this that I posted earlier: Trying to expand / collapse a UITableViewCell from a UIButton in a custom cell

To summarize, I have a UITableView with multiple user table cells. Each custom UITableViewCell has a text area and then a View More button. Essentially, each cell initially displays 3 lines of text, but when the user clicks the View More button, the cell should expand to the entire height of the text area. Pressing the button again will break the cell.

I think this is hard to imagine, so I made a short video here: http://dl.dropbox.com/u/762437/cell-animation.mov

(this is about a 3.5 MB file, so you don’t have to download it for so long). In the video, I show the cell performing the expand / collapse animation. When expanded, the View More button enlivens down. When you click on it again, it simply returns to its original position without animation. Edit: Sorry, I have to be more clear. UITableView correctly animates cells, I ask how to make the "Details" button, the animation is correct.

I have a U-turn / crash working (I did it right or wrong, that's another thing!), But the animation works as I expect. Expanding animation works, but does not collapse.

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 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 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");
                             }];
        }

, "else" if-else buttonRect Y, . , , .

!

+5
1

......

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

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];

... ...

-1

All Articles