简体   繁体   English

TableView CustomCell UIButton问题

[英]TableView CustomCell UIButton ISSUE

I am using a customCell for my tableView.The CustomCell Contains UIButton.And i want to assign an action with this button. 我正在为tableView使用customCell.CustomCell包含UIButton。并且我想使用此按钮分配操作。

BeginingCell.h holds declination for the UIButton as : BeginingCell.h将UIButton的磁偏角保持为:

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

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

@end

And the implementation is : 实现是:

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

I am using this CustomCell in a tableView.I want a response from the button Click.How can i assign a button action for my customCell Button? 我在tableView中使用此CustomCell。我想要按钮Click的响应。如何为我的customCell Button分配按钮动作?

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

Rather than making your button an IBOutlet and trying to wrire it to ur IBAction, just wire it manualy in -(void)viewDidLoad like so: 与其将按钮设为IBOutlet并尝试将其写入URAction,不如将其手动连接到-(void)viewDidLoad如下所示:

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

You can programmatically add an action to the UIButton using the addTarget:action:forControlEvents: method (a method of UIControl, of which UIButton is a subclass). 您可以使用addTarget:action:forControlEvents:方法(UIControl的一种方法,其中UIButton是其子类)以编程方式将操作添加到UIButton。

So your implementation would look something like: 因此,您的实现将如下所示:

[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
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM