imbeginner_sorry imbeginner_sorry - 3 months ago 36
Objective-C Question

UITableViewCell button Animation inside custom cell,i get wrong indexpath of cell to show animation

UITableViewCell button animation inside custom cell,I get wrong indexPath of cell to show animation.

Hi ,I have a problem of my button animation inside UITableView cell .

When I pressed my cell of button I wanted it show the animation,but the visible and last object of button inside UITableView Cell only it can show the animation.

I hope I pressed the button inside UITableView cell that indexPath of 0,and the button of indexPath 0 just it can show the animation.

@interface ListViewController () <UITableViewDataSource,UITableViewDelegate>
{
NSInteger pressedCounts;
}
@property (nonatomic)ListTableViewCell *cell;
@end


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

self.cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];


//Btn setting
[self.cell.listLoveBtn addTarget:self action:@selector(loveBtnPressed:forEvent:) forControlEvents:UIControlEventTouchUpInside];
}

return self.cell;

}


- (IBAction)loveBtnPressed:(id)sender forEvent:(UIEvent *)event{

//set button pressed indexpath

UITouch *touch = event.allTouches.anyObject;
CGPoint touchPoint = [touch locationInView:self.listTableView];
NSIndexPath *indexPath = [self.listTableView indexPathForRowAtPoint:touchPoint];

NSLog(@"btn pressed indexpath is %ld",(long)indexPath.row);


//set button pressed animated

[self.cell.listLoveBtn setImage:[UIImage imageNamed:pressedCounts%2==0?@"listUnlikeBtn":@"listLikeBtn"] forState:UIControlStateNormal];

CAKeyframeAnimation *btnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
btnAnimation.values = @[@(0.1),@(1.0),@(1.5)];
btnAnimation.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
btnAnimation.calculationMode = kCAAnimationLinear;

pressedCounts++;

[self.cell.listLoveBtn.layer addAnimation:btnAnimation forKey:@"Animation"];

}


Thank you!

Answer

You have quite a few issues in your code.

Start by getting rid of the property to reference the cell. It's not doing what you think it is. You also don't need the event for the button tap.

This requires that you update your cellForRowAtIndexPath method as follows:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListTableViewCell *cell = (ListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];

    //Btn setting    
    [cell.listLoveBtn addTarget:self action:@selector(loveBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

For the button handler, you don't need the event. Simply get the button's frame and convert it to the table view's coordinates. Use that to get the cell from the table. And note that the button handler isn't an IBAction.

Also avoid nesting a lot of code into one line. Split lines up. It makes it easier to read and much easier to debug when there is a problem.

- (void)loveBtnPressed:(UIButton *)button {
    CGPoint buttonPoint = CGPointMake(5, 5); // A point within the button
    CGPoint tablePoint = [button convertPoint:buttonPoint toView:self.listTableView]; // Convert point to table view coordinates
    NSIndexPath *indexPath = [self.listTableView indexPathForRowAtPoint:tablePoint]; // Get the index path

    NSLog(@"btn pressed indexpath is %ld",(long)indexPath.row);

    //set button pressed animated
    ListTableViewCell *cell = [self.listTableView cellForRowAtIndexPath:indexPath]; // Get the cell for the index path
    UIImage *image = [UIImage imageNamed:pressedCounts % 2 == 0 ? @"listUnlikeBtn" : @"listLikeBtn"];
    [cell.listLoveBtn setImage:image forState:UIControlStateNormal];

    CAKeyframeAnimation *btnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    btnAnimation.values = @[@(0.1),@(1.0),@(1.5)];
    btnAnimation.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    btnAnimation.calculationMode = kCAAnimationLinear;

    pressedCounts++;

    [cell.listLoveBtn.layer addAnimation:btnAnimation forKey:@"Animation"];
}
Comments