I have four buttons on my CustomTableCell.
Button names are button1
, button2
, button3
, button4
. (as declared in the .h file)
@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;
@property (weak, nonatomic) IBOutlet UIButton *button4;
The following code works fine, and I am generating 4 buttons like this by repeating the code.
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];
Now I ceated an array using the The four buttons will have four different names (not title).
_buttonTitle = [@[@"button1 title",
@"button2 title",
@"button3 title",
@"button4 title"] mutableCopy];
So I thought why not generate the buttons inside a for loop like this.
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];
}
I am getting the following error:
"Property 'buttonName' not found on object of type 'CustomTableViewCell *'"
of course this makes sense as cell.button1 != cell.buttonName
eventhough buttonName
is a NSString
with the value button1
.
So the gist of the question that I am asking is, how can I create a button if I know the button name declared in the .h file?