【问题标题】:Labels with tags inside prototype cells not updating原型单元格内带有标签的标签未更新
【发布时间】:2015-08-02 21:30:23
【问题描述】:

我正在尝试为我当前正在构建的应用程序的一部分创建清单,但我无法使用viewWithTag: 更新原型单元格上的文本。应用程序中的其他所有内容都正常工作,我可以单击正在创建的单元格。

需要使用 viewWithTag 的两种方法是:

- (void)configureCheckmarkForCell:(UITableViewCell *)cell withChecklistItem:(OTChecklistItem *)item {
    UILabel *label = (UILabel *)[cell viewWithTag:1001];

    if (item.checked) {
        label.text = @"√";
    } else {
        label.text = @"";
    }

    label.textColor = self.view.tintColor; }

- (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(OTChecklistItem *)item
{
    UILabel *label = (UILabel *)[cell viewWithTag:1000];
    label.text = item.itemName;
}

这些方法被调用:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get a new or recycled cell
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:@"OTChecklistTableViewCell" forIndexPath:indexPath];

    NSArray *items = [[OTChecklistStore sharedStore] allItems];
    OTChecklistItem *item = [items objectAtIndex:indexPath.row];

    [self configureTextForCell:cell withChecklistItem:item];
    [self configureCheckmarkForCell:cell withChecklistItem:item];

    return cell;
}

这是我的故事板中的一些屏幕截图,显示我的连接和标签应该是正确的。

【问题讨论】:

  • 你的保存方法是空的!! - (void)save:(id)sender { [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; } 只会关闭视图!
  • 对象已经被创建,所以除了关闭视图之外我不需要做任何事情。从表格中创建新对象后,您可以尝试单击单元格,您保存的数据将显示在 detailViewController 中。
  • 不要在您的项目中发布墨迹。使用相关详细信息/代码更新您的问题。

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

我发现了问题。它奏效了:

用于从 cellForRow 方法中提取单元格的标识符是 不同于界面生成器中的一组。看看吧。

更改:

UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:@"OTChecklistTableViewCell" forIndexPath:indexPath];

收件人:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTPlansTableViewCell" forIndexPath:indexPath];

【讨论】:

  • 这对你有用吗?我尝试将其中的 cellIdentifier 更改为 OTChecklistTableViewCell,但它仍然无法正常工作。
  • 您是否完全按照我的建议进行了尝试?!为什么您在界面生成器中更改了该标识符?首先尝试建议的解决方案。然后做你想做的。因为您的代码中也有这个不必要的行: [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"OTChecklistTableViewCell"];
  • 我想说的是,工作代码的格式和结构都很好。
猜你喜欢
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2020-03-14
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2011-11-24
相关资源
最近更新 更多