UITableViewCell animates resize when user lands or touch cell

I want to animate UITableViewCell

like the following image ( Cell is smaller than size smaller ) until showViewController appears. I've tried this from the last two days and tried other answers I've found for similar questions.

I want the cell to animate / resize as soon as the user presses their finger on the UITableViewCell.

enter image description here

UITableViewCell is a custom cell. I tried to set a new frame in cell.contentView in a subclass of UITableViewCell setSelected:(BOOL)selected animated:(BOOL)animated

. The UITableViewCell did not resize as I expected.

What I also observed, this method is called when the user lifts their thumb up from the UITableViewCell. I actually want this to happen as soon as the user clicks on a cell. And if no cell is selected and the user moves to another cell, the cell should be resized to its normal size.

Then I did some searches on getting touch events from the UITableViewCell. And I tried to add UILongPressGestureRecognizer and UITapGestureRecognizer

and in the selector I tried to change the frame of the cell.contentView like this

cell.contentView.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-20, cell.frame.size.height);
[cell setNeedsDisplay];

      

But the selected cell only blinks (cell background), it doesn't change as I expected.

Any suggestions would be really helpful. thanks in advance

+3


source to share


3 answers


Good. After reading some docs and watching videos at WWDC, I was able to fix this issue.

I learned very importantly.

Never use setFrame: when viewed with AutoLayoutEnabled.



With this in mind, I changed my custom UIBableViewCell xib to use AutoResizing masks. And then in my UITableViewController I used these tableView: didHighlightRowAtIndexPath: tableView: didUnHighlightRowAtIndexPath: delegates

Here is a code snippet from my view controller.

        -(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {

        NSLog(@"I'm Highlighted at %ld",(long)indexPath.section);
        CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];

        originalCellSize = cell.frame;

//Using animation so the frame change transform will be smooth.

        [UIView animateWithDuration:0.1f animations:^{

            cell.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-40, cell.frame.size.height);
        }];

    }


    -(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableableView cellForRowAtIndexPath:indexPath];


        [UIView animateWithDuration:0.1f animations:^{
            cell.frame = CGRectMake(0, originalCellSize.origin.y, self.view.bounds.size.width, 180) ;
        }];

        originalCellSize = CGRectMake(0, 0, 0, 0);
    }

      

+2


source


I tried to achieve something similar to you. I was trying to show a portion of my UIImage in a UIImageView that I added to my custom UITableViewCell and set constraints on my image to display the entire cell size (similar to the background, especially since you have a custom cell like Well). I followed @ Joy's answer provided this question and created the following:

I added this to mine viewDidLoad

:

self.currentSelection = -1;
// I changed his line below, so that the image resizing (cell resizing really) is constant for 
//all my images. I guess you can use this to resize your background back and forth to whatever size you want to.
self.newCellHeight = self.tableView.frame.size.width*3/2;

      

Then I added the following:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//I added this to toggle a tap-tapagain kind of thing, so if the same cell is tapped twice, first tap expands and second tap collapses.  
    if (self.currentSelection==indexPath.row) {
        self.currentSelection = -1;
    }else{
        self.currentSelection = indexPath.row;
    }
        // save height for full text label
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];

}
//The rest is pretty much the same as his code
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    // do things with your cell here

    // sentinel
    self.currentSelection = -1;

    // animate
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = self.newCellHeight;
    } else rowHeight = 100;
    return rowHeight;
}

      



Last, if you want to show that the background is centered and "scale" to and from the center. I would set my image mode to center, something like this:

[cell.myImageView setContentMode:UIViewContentModeCenter];

      

I learned a lot from @EmptyStack and @erkanyildiz answers to this question .

Anyway, I hope this helped anyway, and sorry if none of them were relevant! :)

+1


source


try it

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath)path
// or it must be some other method
{
  [self.tableView beginUpdates];
  [[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:rowAnimation];
  [self.tableView endUpdates];
}

      

In the delegate methods in the table view, you need to know which cell is selected.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)path
{
  YourTableClass *cell=[tableView cellForRowAtIndexPath:path];
  if(cell.isTaped)
    return increasedHeight;
  else
    return defaultHeight;
} 

      

-1


source







All Articles