-2

I'm building a survey app with a table view displaying the questions in rows. Some rows have text fields, some have sliders and some have 5 buttons for the user to select their answers.

I can set the initial UIButton values just fine, with multiple cells of buttons and each button can have their titles set. But when I change the data model and reload the tableview, the first row (0) works but the second row (1) does not. The button1.tag returns 0 for the second row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell forIndexPath:indexPath];

NSString *subCatKey = [NSString stringWithFormat:@"%ld", indexPath.row];
NSDictionary *thisSubCat = [_subCatListDict objectForKey:subCatKey];

NSString *defaultResponse = @"";

static NSString *CellIdentifier;
    CellIdentifier = @"buttonCell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
UIButton *button3 = [[UIButton alloc] init];
UIButton *button4 = [[UIButton alloc] init];
UIButton *button5 = [[UIButton alloc] init];

NSLog(@"Button 1::%ld",(long)button1.tag);

// - 4 = Button range
NSLog(@"Setting up the button cell");

NSArray *buttonResponses = [[NSArray alloc] initWithArray:[thisSubCat objectForKey:@"responses"]];

[buttonResponses objectAtIndex:0];

if(cell == nil )
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSLog(@"Button tag 1::%ld",(long)button1.tag);
if (button1.tag == 0) {
    NSLog(@"Setting buttons...");
    button1 = (UIButton *)[cell viewWithTag:41];
    NSLog(@"Button tag 1::%ld",(long)button1.tag);
    button2 = (UIButton *)[cell viewWithTag:42];
    button3 = (UIButton *)[cell viewWithTag:43];
    button4 = (UIButton *)[cell viewWithTag:44];
    button5 = (UIButton *)[cell viewWithTag:45];
    [button1 setTitle:[buttonResponses objectAtIndex:0] forState:UIControlStateNormal];
    [button2 setTitle:[buttonResponses objectAtIndex:1] forState:UIControlStateNormal];
    [button3 setTitle:[buttonResponses objectAtIndex:2] forState:UIControlStateNormal];
    [button4 setTitle:[buttonResponses objectAtIndex:3] forState:UIControlStateNormal];
    [button5 setTitle:[buttonResponses objectAtIndex:4] forState:UIControlStateNormal];

    [button1 addTarget:self
                action:@selector(rowButtonSelect:)
      forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self
                action:@selector(rowButtonSelect:)
      forControlEvents:UIControlEventTouchUpInside];
    [button3 addTarget:self
                action:@selector(rowButtonSelect:)
      forControlEvents:UIControlEventTouchUpInside];
    [button4 addTarget:self
                action:@selector(rowButtonSelect:)
      forControlEvents:UIControlEventTouchUpInside];
    [button5 addTarget:self
                action:@selector(rowButtonSelect:)
      forControlEvents:UIControlEventTouchUpInside];
}


UILabel *questionLabel = (UILabel *)[cell viewWithTag:301];
questionLabel.text = [thisSubCat objectForKey:@"question"];

UILabel *questionResponses = (UILabel *)[cell viewWithTag:302];
questionResponses.text = defaultResponse;
NSLog(@"Default response:%@",defaultResponse);


return cell;

}

jreynolds
  • 27
  • 2
  • try to remove your if(button1.tag == 0) condition or add else part what you want when if condition become false. May this will help you. – Rohit Khandelwal Mar 11 '16 at 12:00
  • You create the button, and then check its tag. Of course the tag value is zero-- that's its default value. It is not clear what you're trying to do here. Also, this is a hackneyed way to go about setting up cells. – AMayes Mar 11 '16 at 12:08
  • @Dev.RK I added the if(button1.tag ==0) because the table never scrolls and they shouldn't get reused. Originally, I didn't have this, but thought I would try to skip it if the cell wasn't reused. – jreynolds Mar 11 '16 at 13:34
  • @AMayes when the table is first presented, all of these work and the button tags are set properly. It's just on a tableView reloadData that this doesn't work. – jreynolds Mar 11 '16 at 13:36

1 Answers1

0

You always Create new Button in cell so it's default tag = 0 so

NSLog(@"Button tag 1::%ld",(long)button1.tag);

will print Button tag 1::0

set

button1.tag indexpath.raw;

and try

  • Right, the real test is after: button1 = (UIButton *)[cell viewWithTag:41]; After the reloadData, on the second cell (row 1) button1.tag is still 0, even though button1 in the cell on row 0 equals 41, like it should be. – jreynolds Mar 11 '16 at 16:04