【问题标题】:Checkmark won't show in TableViewCell on iOS7iOS7 上的 TableViewCell 中不会显示复选标记
【发布时间】:2013-10-08 13:43:03
【问题描述】:

我现在正在处理一个奇怪的问题。我的 Apps Deployment Target 设置为 iOS6,所以我想同时支持 iOS6 和 iOS7。

我只有一个简单的 UITableView,用户可以在其中选择喜欢的通知声音。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CheckmarkCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        [cell setTintColor:[UIColor redColor]];
        if (indexPath.section == 0){
            cell.textLabel.text = [_availableSounds objectAtIndex:indexPath.row];
            if (indexPath.row == _checkedSoundIndexPath.row) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
        else {
// Unrelated, another settings cell
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }

我的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) {
        return;
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    if (_checkedSoundIndexPath != indexPath) {
        [[self.tableView cellForRowAtIndexPath:_checkedSoundIndexPath] setAccessoryType:UITableViewCellAccessoryNone];
    }
    _checkedSoundIndexPath = indexPath;
}

问题是 iOS7 iPhone 不会按预期显示复选标记。在 iOS6 iPhone 上运行相同的代码可以按预期工作。我试图插入[cell setTintColor:[UIColor redColor]];,但没有任何运气。即使我删除所有与 AccessoryType 相关的代码并在我的故事板中添加复选标记,也不会出现任何内容。请看下面的截图(第一个是iOS6,第二个是iOS5)。

有人有想法吗?还是 iOS7 的 bug?

提前致谢!

编辑:

即使我制作了一个新的简单 UITableViewController,只有 5 个将 Accessory 设置为 UITableViewAccessoryTypeCheckmark 的单元格,iOS7 上也不会出现 Checkmarks。

【问题讨论】:

  • 另一个说明。 UITableViewCellAccessoryCheckmark 和 UITableViewCellAccessoryDisclosureIndicator 不会出现在我的应用程序的任何地方。 UITableViewCellAccessoryDe​​tailDisclosureButton 和 UITableViewCellAccessoryDe​​tailButton 按预期工作。
  • 很高兴,我不是唯一一个面临这个问题的人。 :-)
  • UITableViewCell 有自定义子类吗? (你可以在 Interface Builder 中看到)
  • 不,它是一个具有基本样式的 UITableViewCell。
  • 我遇到了同样的问题:最初为 iOS 5 开发的项目,复选标记和披露指示器仅在 iOS 7 上不可见,尽管详细披露按钮和详细信息按钮工作正常。到目前为止,我已经尝试了建议的修复程序,但没有运气。观众,你有没有找到解决办法? (我总是可以使用附件视图来显示我自己的复选标记和披露图像,但这似乎是一个讨厌的 hack。)

标签: ios6 uitableview ios7


【解决方案1】:

我有一个类似的问题,我解决了这个问题,改变了 uitableview 的色调

我将 InterfaceBuilder 的 uitable 的 tintcolot 更改为默认颜色

tableView.tintColor =  [UIColor blackColor];

【讨论】:

  • 不适合我。 :-( tableView.tinitColor 和 cell.tintColor 仍然没有运气。附件不会出现。它在你的应用程序中工作吗,giuseppe?
  • 正确。基本上 tableView.tintColor 属性需要设置为除了故事板或笔尖中的“默认”之外的任何颜色。我已将其设置为蓝色,现在没有任何问题。
  • 很好的答案,谢谢!我以编程方式创建了一个 UITableView 并且 UITableViewCellAccessoryCheckmark 总是白色的。感谢分享。
【解决方案2】:

很长一段时间以来,我都遇到了与您完全相同的问题。 就我而言,我实施了一些外观调整。其中之一是

[[UIView appearance] setTintColor:[UIColor whiteColor]];

尝试在您的项目中找到类似的全局内容。

【讨论】:

    【解决方案3】:
    In my application it working perfect,check it 
    
    //take it in .h file mutable arSelectedRows;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *CellIdentifier = @"Cell";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       if (cell == nil) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
       [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    
       //Do anything you want for cell here
       if([arSelectedRows containsObject:indexPath]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
       else {
              cell.accessoryType = UITableViewCellAccessoryNone;
        }
       return cell;
    }
    
     #pragma mark - Table view delegate
    
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       if(cell.accessoryType == UITableViewCellAccessoryNone) {
           cell.accessoryType = UITableViewCellAccessoryCheckmark;
           [arSelectedRows addObject:indexPath];
       }
       else {
          cell.accessoryType = UITableViewCellAccessoryNone;
           [arSelectedRows removeObject:indexPath];
      }
       NSLog(@"id are here :%@",arSelectedIDs);
    
       [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    

    可能会有帮助。

    【讨论】:

    • 与我的代码基本相同的逻辑。尝试使用您的代码创建一个新的 UITableViewController 类,但选中标记仍然没有运气。 UITableViewCellAccessoryCheckmark 和 UITableViewCellAccessoryDisclosureIndicator 不会出现在我的应用程序中。不过还是谢谢。
    • 这个解决方案非常接近我的答案,但缺少一件重要的事情。另外,我会在这里完成所需的更正,但我不建议在其他方法中使用 cellForRowAtIndexPath: 方法调用来获取数据。查看我的答案,并随时在那里发表评论。
    【解决方案4】:

    不,iOS 7 中的 UITableViewCellAccessoryCheckmark 没有问题,请确保您已为其实现了正确的逻辑。

    【讨论】:

    • 它在iOS6上运行,所以我认为逻辑是正确的。也许您可以帮助找出我的代码中的错误。
    • 我已经用 UITableViewCellAccessoryCheckmark 检查了 tableview,它显示完美。如果您想尝试使用带有 tableview 的 ios7 创建简单的应用程序并将那里的 accessoryType 设置为 UITableViewCellAccessoryCheckmark。
    • 如果我创建一个新项目,一切正常。这有点奇怪。但我无法从头开始重写项目。
    • 我已在问题中添加了注释。我也尝试过创建一个新课程,但不会出现复选标记。
    【解决方案5】:

    我认为问题出在您的 checksoundIndexpath 上,请检查它是否有正确的 indexpath,或者首先检查硬编码的 indexpath。

    您没有初始化 checksoundindxpath。

    我还注意到您没有将选定的行 indexpath 分配给 _checkSoundIndexpath,仅检查两个 indexpath(当前 indexpaths 和 _checksoundindexpath)是否相等,但如果它们不同,则应将选定的 indedxpath 分配给 _checksound indexpath。

    【讨论】:

    • 我在视图控制器-init中初始化变量。即使我像_checkedSoundIndexPath = [NSIndexPath indexPathForItem:0 inSection:0]; 那样对其进行硬编码,也不会出现复选标记。复制代码时,我错过了- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中的一行。无论如何感谢您的回答。
    • 好吧,我不确定,但是在 willDisplaycell: atIndexpath: 方法中尝试 [cell bringSubViewToFront:cell.accessoryView]
    【解决方案6】:

    我可以通过设置 Cell 的 tint color 来解决这个问题 它没有显示,因为单元格色调为白色,并且单元格选择样式为无

    cell.tintColor = [UIColor blackColor];
    
        if(cell.selected){
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        }else{
            [cell setAccessoryType:UITableViewCellAccessoryNone];
    
        }
    

    【讨论】:

      【解决方案7】:

      didSelectRowAtIndexPath: 方法中设置适当的数据后,我宁愿重新加载表格视图(最好只加载受影响的行 - 使用reloadRowsAtIndexPaths)。

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      // Other logic..
      
                  [tableView deselectRowAtIndexPath:indexPath animated:NO]; // Would like to look for an alternative method to avoid this refresh??
      
                  NSMutableArray *reloadArray = [NSMutableArray arrayWithCapacity:2];
                  [reloadArray addObject:indexPath];
      
                  [reloadArray addObject:_checkedSoundIndexPath];
      
                  self.checkedSoundIndexPath = indexPath;   // Should set this before reload
                  [tableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationAutomatic];
      }
      

      而且,最重要的是,在 cellForRowAtIndexPath: 方法中添加这些行 -

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          // All your other code for this section followed by these lines...
      
          if([_checkedSoundIndexPath compare:indexPath] == NSOrderedSame) {
              cell.accessoryType = UITableViewCellAccessoryCheckmark;
          }
          else {
              cell.accessoryType = UITableViewCellAccessoryNone;
          }
      
          // Other sections code
          return cell;
      }
      

      【讨论】:

      • 感谢您提供比较索引路径和重新加载受影响单元格的提示,但选中标记仍然没有运气。可笑,它不会出现。
      【解决方案8】:

      我知道原始问题使用了一个简单的UITableViewCell,但这可能适用于具有自定义表格视图单元格的其他人。

      我有一个自定义的UITableViewCell 子类。我尝试了一切,但复选标记不会显示。最终我意识到我覆盖了layoutSubviews,但忘了打电话给[super layoutSubviews]。这意味着复选标记将无法正确显示。

       - (void)layoutSubviews
      {
          [super layoutSubviews];
          //...custom layout here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        • 2013-09-29
        相关资源
        最近更新 更多