2011-04-18 57 views
1

我为我的tableView使用customCell。CustomCell包含UIButton.And我想用此按钮分配一个操作。TableView CustomCell UIButton ISSUE

BeginingCell.h持有偏角为的UIButton为:

#import <UIKit/UIKit.h> 
@interface BeginingCell : UITableViewCell { 
    IBOutlet UIButton *ansBtn1; 
} 

@property(nonatomic,retain)IBOutlet UIButton *ansBtn1; 

@end 

和实现是:

// 
// BeginingCell.m 
// customCells 
// 

#import "BeginingCell.h"  

@implementation BeginingCell 

///########### Synthesize ///########### 

@synthesize ansBtn1; 

///########### Factory ///########### 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state. 
} 

///########### End Factory ///########### 

- (void)dealloc { 
    [super dealloc]; 
} 
@end 

我用这CustomCell在tableView.I要从按钮点击的响应。如何为我的customCell按钮分配按钮动作?

回答

1
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton addTarget:self 
      action:@selector(myButtonAction:) 
    forControlEvents:UIControlEventTouchUpInside]; 
[myButton setTitle:@"Action" forState:UIControlStateNormal]; 
myButton.frame = CGRectMake(buttonFrame); 
[self addSubview:myButton]; 
+0

什么是这里的buttonFrame? – 2011-04-19 04:20:24

+0

你想要放置按钮的框架。它指定矩形的坐标,例如:0,0,20,20 – visakh7 2011-04-19 04:21:54

+0

我在这里没有使用任何框架.BeginingCell是我的customButton类。并且我试图获得响应从它我在我的tableView显示我的customCell开始细胞 – 2011-04-19 04:31:27

0

而不是让你的按钮,一个IBOutlet,并试图wrire它UR IBAction为,刚丝它manualy在-(void)viewDidLoad像这样:

[ansBtn1 addTarget:self action: @selector(myActionMethod:) 
     forControlEvents:UIControlEventTouchUpInside]; 
+0

我应该把这段代码放在哪里?在customCell实现或者TableView实现中? – 2011-04-18 12:33:13

+0

在哪个实现中我应该放这个代码? – 2011-04-18 12:40:00

+0

在你的表视图的implimentation。当你想创建你想要包含按钮的单元格时,将该按钮添加为子视图,然后使用addTarget:action:forControlEvents: – Sabobin 2011-04-18 13:10:34

0

您可以用编程方式添加一个动作到的UIButton addTarget:action:forControlEvents:方法(UIControl的一种方法,其中UIButton是一个子类)。

所以您的实现看起来是这样的:

[ansBtn1 addTarget:self 
      action:@selector(btnAction) 
    forControlEvents:UIControlEventTouchUpInside]; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

      [ansBtn1 addTarget:self 
      action:@selector(btnAction:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return self; 
} 
//Buttons Action 
-(void) btnAction:(id) sender 
{ 

} 
+0

我应该把这段代码放在哪里?在BeginingCell(CustomCell)实现中或者在tablViewImplementation? – 2011-04-18 12:35:02

+0

在BeginingCell类的initWithStyle功能...只需使用写在我的答案.. – Jhaliya 2011-04-18 12:36:07

+0

我把这样的代码://按钮动作 - (空)btnAction:(ID)发送 { \t的NSLog(@ “测试” ); } /// ###########厂/// ########### - (ID)initWithStyle:(UITableViewCellStyle)风格reuseIdentifier:(NSString *)reuseIdentifier { \t self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 如果(个体){ \t \t \t \t [ansBtn1 addTarget:自 \t \t \t \t \t动作:@selector(btnAction :) forControlEvents:UIControlEventTouchUpInside]; } return self; }但没有任何情况发生。我通过调试进行了测试。 – 2011-04-18 12:47:43