2014-05-16 71 views
0

我试图在用户按下单元格中的按钮时自定义UITableViewCell中的一些项目。我已经设置了addTarget:并添加了一个方法来为单元格中的项目设置动画。我为按钮设置了一个标签,这样我就可以得到索引了。在该方法中,我调用cellForRowAtIndexPath:获取调用该按钮的单元格。我正在将由cellForRowAtIndexPath返回的对象投射到我的自定义单元格中。在定制单元格之后,我会在对象上执行动画。问题是动画没有发生。当我尝试在单元格中的某个项目上设置属性时,它也不起作用。自定义单元格不返回零,所以我不确定这个问题。我究竟做错了什么?在自定义UITableViewCell上执行动画

这里是我使用调用动画

-(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

请包括您所使用调用动画 – jrturton

+0

代码可能要打印我们的索引路径行,以确保实际上对于您所按的单元格有正确的indexPath。 – Zhang

+0

@jrturton添加代码。 – Milo

回答

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 
ViewController.h文件

#import <UIKit/UIKit.h> 
#import "FavoritesCell.h" //import it 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CellActionDelegate> //confirms to delegate 

//.... other stuffs 
在ViewController.m

文件

//...other stuffs 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FavoritesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 
    if(cell == nil) 
    { 
     cell = [[FavoritesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; 

    } 

    cell.cellDelegate = self; //set the deleagte to this class 
    [cell.picButton setImage:[UIImage imageNamed:@"Two-Red-Flower-.jpg"] forState:UIControlStateNormal]; //set it hear or in custom cell itself 
    //...other logics 

    return cell; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 130.0f; 
} 

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


} 

//define the delegate method 
- (void)doAnimationForCell:(FavoritesCell *)cell forButton:(UIButton *)picButton //this the picButton 
{ 

    //you can get index path of cell 
    //you can get the tapped button of the cell 
    //then do your animation 
    [UIView animateWithDuration:0.2 animations:^{ 
     picButton.alpha = 0.5; 
    } completion:nil]; 

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