当用户按下单元格中的按钮时,我正在尝试为自定义UITableViewCell
中的一些项目制作动画。我设置了addTarget
:并添加了一种方法来为单元格中的项目设置动画。我为按钮设置了一个标签,这样我就可以得到索引。在方法中,我调用cellForRowAtIndexPath
:来获取按钮被调用的单元格。我正在将cellForRowAtIndexPath
返回的对象转换为我的自定义单元格。拥有自定义单元格后,我会对对象执行动画。问题是动画没有发生。当我尝试在单元格中的一个项目上设置属性时,它也不起作用。自定义单元格没有返回零,所以我不确定这个问题。我做错了什么?
以下是我用来调用动画的代码
-(void)favButtonPressed:(id)sender{
FavoritesCell *favCell = (FavoritesCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
[UIView animateWithDuration:0.2 animations:^{
favCell.picButton.alpha = 0.5;
} completion:nil];
}
答案 0 :(得分:0)
你可以通过使用下面的自定义委托来实现这一点,希望这有助于你。
我举了一个你的案例的例子,希望这有助于你
在自定义单元格calss中定义委托协议,如下所示
在FavoritesCell.h
#import <UIKit/UIKit.h>
@class FavoritesCell;
@protocol CellActionDelegate <NSObject>
- (void)doAnimationForCell:(FavoritesCell *)cell forButton:(UIButton *)picButton; //in this u can pass the cell itself
@end
@interface FavoritesCell : UITableViewCell
@property (nonatomic,retain)UIButton *picButton; //i am doing a simple test, say this is your pic button
@property (nonatomic,assign) id<CellActionDelegate>cellDelegate;
@end
并在FavoritesCell.m
档案
#import "FavoritesCell.h"
@implementation FavoritesCell
@synthesize cellDelegate;
@synthesize picButton = _picButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_picButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 3, 100, 100)];
//set the property // for test
[_picButton addTarget:self action:@selector(favButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[_picButton.layer setCornerRadius:50];
[_picButton.layer setMasksToBounds:YES];
[_picButton.layer setBorderColor:[[UIColor blackColor]CGColor]];
[_picButton.layer setBorderWidth:5];
_picButton.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:_picButton];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)favButtonPressed:(UIButton *)sender
{
//or you can do the animation hear only no need to delegate to controller form cell
if([self.cellDelegate respondsToSelector:@selector(doAnimationForCell:forButton:)])
{
[self.cellDelegate doAnimationForCell:self forButton:sender]; //call this method
}
}
@end
<{1>}文件中的
ViewController.h
ViewController.m文件中的
#import <UIKit/UIKit.h>
#import "FavoritesCell.h" //import it
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CellActionDelegate> //confirms to delegate
//.... other stuffs
答案 1 :(得分:0)
我想你想要这样的东西:
-(void)favButtonPressed:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
// or try this one alternatively if the above line give wrong indexPath
//CGPoint buttonPosition = [sender convertPoint:((UIButton *)sender).center toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
// I assume "self" here is your view controller
FavoritesCell *favCell = (FavoritesCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
[UIView animateWithDuration:0.2 animations:^{
favCell.picButton.alpha = 0.5;
} completion:nil];
}