我有一个tableview控制器类,一个tableviewcell子类和一个uibutton子类。我在tableviewcell子类中创建unbutton子类的实例,并在特定单元格位置初始化按钮。然后,我在tableview控制器类中使用此单元格。另外,我试图将IBAction添加到按钮。但是,在其他一切正常的情况下,它无法识别uibutton子类的对象。我在声明中做错了什么?
tableviewcell.m
#import "tableviewcell.h"
#import "CustomCheckButton.h"
CustomCheckButton *starbtn = [[CustomCheckButton alloc] init];;
starbtn = [[CustomCheckButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
tableviewcontroller.m
在cellForRowAtIndexPath对象中,不会识别startbtn:
#import "ScannedProductControllerViewController.h"
#import "imageCellCell.h"
tableviewcell*firstRowCell = (tableviewcell *)cell;
[firstRowCell.prodimage setImage: [UIImage imageNamed:@"test1.jpg"]];
[firstRowCell.label1 setText:@"17.5"];
[firstRowCell.label2 setText:@"Score"];
[firstRowCell.basket setImage: [UIImage imageNamed:@"Basket.jpg"]];
// reference of the home button to the buttonclick method
[firstRowCell.homebtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
// reference of the favorites button to the buttonclick method
[firstRowCell.starbtn addTarget:self action:@selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
CustomCheckButton.h
#import <UIKit/UIKit.h>
@interface CustomCheckButton : UIButton {
BOOL _checked;
}
@property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
@end
@interface TableViewCell : UITableViewCell
@property (nonatomic, strong) CustomCheckButton *startButton;
@end
@implementation TableViewCell
- (instancetype)init {
self = [super init];
self.startButton = [[CustomCheckButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.contentView addSubview:self.startButton];
}
@end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句