我正在构建一个调查应用程序,其中包含以行显示问题的表格视图。有些行有文本字段,有些行有滑块,有些行有5个按钮供用户选择答案。
我可以设置初始UIButton值,使用多个按钮单元格,每个按钮都可以设置标题。但是当我更改数据模型并重新加载tableview时,第一行(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;
}
答案 0 :(得分:0)
您总是在单元格中创建新按钮,因此它的默认标记= 0所以
NSLog(@"Button tag 1::%ld",(long)button1.tag);
将打印Button标签1 :: 0
设置
button1.tag indexpath.raw;
并尝试