一种方式ü可以使用自定义委托像下面这样做的代码,希望这有助于ü。
我把你的情况的一个例子通过这个希望这有助于ü
在自定义单元格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];
}
请包括您所使用调用动画 – jrturton
代码可能要打印我们的索引路径行,以确保实际上对于您所按的单元格有正确的indexPath。 – Zhang
@jrturton添加代码。 – Milo