Xcode:如何查找哪个单元格没有重用标识符

时间:2018-07-31 14:05:07

标签: swift uitableview reuseidentifier

我有这个警告:

原型表单元必须具有重用标识符

该项目非常大,我们有很多tableViews,但我找不到导致此警告的单元格。有没有办法找到此警告的来源?我可以看到情节提要是Friends.Storyboard,但其中的所有单元格都有一个ID。

enter image description here

3 个答案:

答案 0 :(得分:2)

使用左Clic打开Friends.storyboard打开为/源代码。 您现在应该将Friends.storyboard视为XML文件。

我专门创建了一个UITableView,其中有两个单元格,一个单元格具有重用标识符,而另一个单元格没有一个单元格:

<prototypes>
    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="thisID" id="rXT-Vv-RiK">
        <rect key="frame" x="0.0" y="28" width="327" height="44"/>
        <autoresizingMask key="autoresizingMask"/>
        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="rXT-Vv-RiK" id="o6B-Eq-gDX">
            <rect key="frame" x="0.0" y="0.0" width="327" height="43.5"/>
            <autoresizingMask key="autoresizingMask"/>
        </tableViewCellContentView>
    </tableViewCell>
    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="R2l-ad-LaA">
        <rect key="frame" x="0.0" y="72" width="327" height="44"/>
        <autoresizingMask key="autoresizingMask"/>
        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="R2l-ad-LaA" id="zNB-8w-4eM">
            <rect key="frame" x="0.0" y="0.0" width="327" height="43.5"/>
            <autoresizingMask key="autoresizingMask"/>
        </tableViewCellContentView>
    </tableViewCell>
</prototypes>

简化(指出要查找的内容)

...
<tableViewCell ... reuseIdentifier="thisID" ...>
</tableViewCell>
<tableViewCell ...> //With no attribute/tag "reuseIdentifier"
</tableViewCell>
...

因此,寻找<tableViewCell应该是一个好的开始。 您也可以使用RegularExpression,但是与“手动”搜索的用法相对比,这可能工作太多(无法进行过多的测试检查)。

答案 1 :(得分:2)

  1. 构建您的项目(因此会生成警告)
  2. 转到报告导航器
  3. 单击带有警告的构建日志

enter image description here

  1. 在构建日志中展开警告
  2. 您现在将看到有问题的单元格的ID

enter image description here

  1. 使用“在项目中查找”在您的项目中搜索刚刚找到的ID。
  2. 点击返回的结果

enter image description here

  1. 故事板将打开,并选择正确的单元格,您可以添加重用标识符

答案 2 :(得分:0)

如果您不介意使用终端和正则表达式,这里还有另一个选择,它可以使整个项目范围内的搜索速度非常快。

这利用了index.html,它是支持Perl兼容正则表达式的grep版本。

如果没有pcregrep,则可以使用brew安装它:

pcregrep

安装后,您可以进入项目根目录并在终端中运行此命令,(请注意尾随brew install pcre ):

.

它将列出声明tableViewCell且缺少重用标识符的文件名和行号。

它将在终端中打印出如下结果:

pcregrep -rnI --buffer-size=100000000 '^(.)*\btableViewCell \b((?!reuseIdentifier).)*$' .

说明:

  • pcregrep-该程序将解析源文件以查找匹配项。
  • -rnI-(r)递归搜索-(n)在匹配文件中打印行号。 (I)忽略二进制文件。
  • -buffer-size = 100000000-设置较大的缓冲区以解析较大的文件。 (对于我项目中的某些文件是必需的)

正则表达式:^(。) \ btableViewCell \ b((?!reuseIdentifier)。) $

  • ^-在行首的匹配位置
  • (。)*-在行的开头无限次匹配任何内容
  • \ btableViewCell \ b-完全匹配单词'tableViewCell'与尾随空格
  • ((?! reuseIdentifier)。)-匹配未找到'reuseIdentifier'的行
  • * $-后跟任意文本和该行的结尾
相关问题