-2

我正在构建一个带有表格视图的调查应用程序,表格视图按行显示问题。有些行有文本字段,有些行有滑块,有些行有 5 个按钮供用户选择答案。

我可以很好地设置初始 UIButton 值,使用多个按钮单元格,并且每个按钮都可以设置其标题。但是当我更改数据模型并重新加载表格视图时,第一行 (0) 有效,但第二行 (1) 无效。button1.tag 为第二行返回 0。

- (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;

}

4

1 回答 1

0

你总是在单元格中创建新按钮,所以它是默认标签 = 0 所以

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

将打印按钮标签 1::0

button1.tag indexpath.raw;

并尝试

于 2016-03-11T12:19:12.390 回答