【问题标题】:viewWithTag UIButton not working on tableview reloadDataviewWithTag UIButton 在 tableview reloadData 上不起作用
【发布时间】:2016-03-11 11:54:56
【问题描述】:

我正在构建一个带有表格视图的调查应用程序,表格视图按行显示问题。有些行有文本字段,有些行有滑块,有些行有 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;

}

【问题讨论】:

  • 尝试删除您的 if(button1.tag == 0) 条件或在 if 条件变为 false 时添加您想要的 else 部分。希望这会对你有所帮助。
  • 您创建按钮,然后检查其标签。当然,标签值为零——这是它的默认值。目前尚不清楚您要在这里做什么。此外,这是设置单元格的陈旧方法。
  • @Dev.RK 我添加了 if(button1.tag ==0) 因为表格永远不会滚动并且它们不应该被重用。本来我是没有这个的,但是想着如果不重用单元格我会尝试跳过它。
  • @AMayes 当表格首次出现时,所有这些工作和按钮标签设置正确。只是在 tableView reloadData 上这不起作用。

标签: ios objective-c uitableview uibutton viewwithtag


【解决方案1】:

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

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

将打印按钮标签 1::0

设置

button1.tag indexpath.raw;

试试

【讨论】:

  • 对,真正的测试是在:button1 = (UIButton *)[cell viewWithTag:41];在 reloadData 之后,在第二个单元格(第 1 行)上 button1.tag 仍然为 0,即使第 0 行单元格中的 button1 等于 41,就像它应该的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多