【发布时间】:2014-09-11 07:41:49
【问题描述】:
我的 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];
现在我使用四个按钮创建了一个数组,将有四个不同的名称(不是标题)。
_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 尽管buttonName 是一个NSString 的值button1。
所以我要问的问题的要点是,如果我知道 .h 文件中声明的按钮名称,我该如何创建一个按钮?
【问题讨论】:
标签: ios