-2

質問を行で表示するテーブル ビューを備えた調査アプリを構築しています。一部の行にはテキスト フィールドがあり、一部にはスライダーがあり、一部にはユーザーが回答を選択するための 5 つのボタンがあります。

ボタンの複数のセルを使用して、UIButton の初期値を問題なく設定でき、各ボタンにタイトルを設定できます。しかし、データ モデルを変更してテーブルビューをリロードすると、最初の行 (0) は機能しますが、2 行目 (1) は機能しません。button1.tag は、2 行目に 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