0

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?

Larme
  • 24,190
  • 6
  • 51
  • 81
santoshaa
  • 173
  • 2
  • 4
  • 10

3 Answers3

2

Are you wondering how to create a button programmatically? If so:

UIButton *btn = [[UIButton alloc] init];
[btn setTitle:@"YOUR TITLE" forState:UIControlStateNormal];

If you're looking to set a button's title and that title is stored in an array, you could do something like this:

UIButton *btn = [[UIButton alloc] init];
[btn setTitle:[NSString stringWithFormat:@"%@", [self.yourButtonTitleArray objectAtIndex:index]] forState:UIControlStateNormal];
trevorj
  • 2,029
  • 1
  • 16
  • 11
0

buttonName is not a button it's a string.

Try to set the tag for each button, like button1 with tag 0, button2 with tag 1 and so on. You can set it in the interface of your view (.xib file)

and then update your code to the following, it should work:

for (int i = 0; i <= _buttonTitle.count ; i++) {
    NSString *buttonNumber = [NSString stringWithFormat:@"%d", i+1];
    //I removed this
    //NSString *buttonName = [@"button" stringByAppendingString:buttonNumber];
    //I added this
    UIButton* currentButton = (UIButton*)[cell viewWithTag:i];

    [currentButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
    currentButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    currentButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    currentButton.titleLabel.numberOfLines = 0;
    //      NSLog(buttonName);
    [currentButton setTitle:_buttonTitle[i] forState:UIControlStateNormal];
    [currentButton setSelected:NO];
}
Sawsan
  • 1,096
  • 10
  • 20
0

Considering what you are trying to achieve i recommend using run time property creation if number of buttons are to be veried. See this Question for details. However if you have only these four buttons to display in the cell, don't try anything fancy. It is perfectly ok to have static property declaration as you are using in your first approach.

Community
  • 1
  • 1
Gandalf
  • 2,399
  • 2
  • 15
  • 19