【问题标题】:Xcode: How to find which cell has no reuse identifierXcode:如何找到没有重用标识符的单元格
【发布时间】:2018-07-31 14:05:06
【问题描述】:

我有这个警告:

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

该项目非常大,我们有很多tableViews,我找不到导致此警告的单元格。有没有办法找到这个警告的来源?我可以看到故事板是 Friends.Storyboard,但那里的所有单元格都有一个 ID。

【问题讨论】:

  • Open As/Source Code 的身份打开故事板(在其上单击鼠标左键)。然后,您应该会看到一个作为 XML 的情节提要。查找 <tableViewCell 并在同一个 XML 属性/标签中,即没有 reuseIdentifier="someID" 的那个。您也可以在该 XML 中使用正则表达式。
  • @Larme 这应该是一个答案。

标签: swift uitableview reuseidentifier


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

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

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

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

【讨论】:

【解决方案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>
...

所以寻找&lt;tableViewCell 应该是一个好的开始。 您也可以使用正则表达式,但与使用“手动”搜索相比,这可能工作量太大(检查它的测试太多)。

【讨论】:

    【解决方案3】:

    如果您不介意使用终端和正则表达式,这是另一种选择,它使项目范围内的搜索变得非常快。

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

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

    brew install pcre
    

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

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

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

    它会像这样在终端中打印出结果:

    // this is the terminal prompt and the command we entered to search
    myUser$ pcregrep -rnI --buffer-size=10000000000 '^(.)*\btableViewCell \b((?!reuseIdentifier).)*$' .
    
    // This is the path to the matched file, followed by the line number with the match, followed by a preview of the matched line
    ./MyProject/Source/UI/MyController/Base.lproj/MyAccountTableViewCell.xib:13:        <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" rowHeight="71" id="B6A-LV-CH7" customClass="MyAccountTableViewCell">
    

    解释:

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

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

    • ^ - 匹配行首的位置
    • (.)* - 无限次匹配行首的任何内容
    • \btableViewCell \b - 匹配单词 'tableViewCell' 和尾随空格
    • ((?!reuseIdentifier).) - 匹配未找到“reuseIdentifier”的行
    • *$ - 后跟任何文本和行尾

    【讨论】:

    • 我不得不运行 pcre2grep 而不是 pcregrep,因为后者因分段错误而崩溃,但否则您的解决方案可以完美运行。
    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2010-10-13
    • 2016-05-12
    • 2015-10-05
    相关资源
    最近更新 更多