簡體   English   中英

TableView CustomCell UIButton問題

[英]TableView CustomCell UIButton ISSUE

我正在為tableView使用customCell.CustomCell包含UIButton。並且我想使用此按鈕分配操作。

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

我在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];

與其將按鈕設為IBOutlet並嘗試將其寫入URAction,不如將其手動連接到-(void)viewDidLoad如下所示:

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

您可以使用addTarget:action:forControlEvents:方法(UIControl的一種方法,其中UIButton是其子類)以編程方式將操作添加到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
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM