我的CustomTableCell上有四個按鈕。 按鈕名,button1
,button2
,button3
,button4
。 (在.h文件中聲明)如何以編程方式生成按鈕名稱按鈕
@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;
@property (weak, nonatomic) IBOutlet UIButton *button4;
以下代碼正常工作,並且通過重複代碼生成4個像這樣的按鈕。
CustomTableViewCell *cell = [self.tableVIew dequeueReusableCellWithIdentifier:@"CustomCell"];
[cell.button1.titleLabel setFont:[UIFont systemFontOfSize:13]];
cell.button1.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.button1.titleLabel.textAlignment = NSTextAlignmentLeft;
cell.button1.titleLabel.numberOfLines = 0;
[cell.button1 setTitle:@"button1 title" forState:UIControlStateNormal];
[cell.button1 setSelected:NO];
現在我使用的四個按鈕將有四個不同的名稱(不是標題)ceated陣列。
_buttonTitle = [@[@"button1 title",
@"button2 title",
@"button3 title",
@"button4 title"] mutableCopy];
所以我想爲什麼不在這樣的for循環中生成按鈕。
for (int i = 0; i <= _buttonTitle.count ; i++) {
NSString *buttonNumber = [NSString stringWithFormat:@"%d", i+1];
NSString *buttonName = [@"button" stringByAppendingString:buttonNumber];
[cell.buttonName.titleLabel setFont:[UIFont systemFontOfSize:13]];
cell.buttonName.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.buttonName.titleLabel.textAlignment = NSTextAlignmentLeft;
cell.buttonName.titleLabel.numberOfLines = 0;
// NSLog(buttonName);
[cell.buttonName setTitle:_buttonTitle[i] forState:UIControlStateNormal];
[cell.buttonName setSelected:NO];
}
我收到以下錯誤:當然
"Property 'buttonName' not found on object of type 'CustomTableViewCell *'"
,這是有道理的cell.button1 != cell.buttonName
eventhough buttonName
是NSString
與價值button1
。
於是,我問的是這個問題,我怎麼可以創建一個按鈕,如果我知道在.h文件中聲明的按鈕名稱的要點?