菜鸟教程小白 发表于 2022-12-11 20:20:19

ios - UITableViewCell 自定义editingAccessoryView - 未正确关闭


                                            <p><p>我已经实现了自定义编辑附件 View ,如我对 <a href="https://stackoverflow.com/questions/7295834/custom-editingaccessoryview-not-working" rel="noreferrer noopener nofollow">this</a> 的回答中所述。问题。在大多数情况下,它工作得很好,但我注意到它有一个小问题。 </p>

<p>当我在表格 View 中滚动或选择另一行时,我的自定义编辑附件不会被关闭。使用标准编辑附件(删除按钮),可以捕获桌面上任何其他位置的触摸并用于删除删除附件 View- 例如,您可以在内置的 Notes 应用程序中自己看到这一点,或者在任何其他地方使用标准的编辑附件 View 。 </p>

<p>这一定是因为我在滑动删除模式时返回 <code>UITableViewEditingStyleNone</code>。但是,如果我返回任何其他模式,则不会显示我的自定义编辑附件。 </p>

<p>如何恢复标准编辑风格的功能,即触摸表格 View 上的任何位置都会关闭编辑附件? </p>

<p>单元格没有子类化,但它是从具有自定义布局的 nib 文件加载的。编辑附件 View 是 nib 文件的一部分,并通过editingAccessoryViewsocket 连接。 </p>

<p>通过存储滑动编辑行的索引路径并在选择另一行或在表格上开始滚动时将该单元格设置为退出编辑模式,我已经成功地实现了我想要的效果。但是,我想正确地做到这一点。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我能够解决这个问题,但遗憾的是它需要额外的工作,而且不像设置几个属性那么简单。</p>

<p>在我的</p>

<pre><code>- (UITableViewCellEditingStyle)tableView:(UITableView *)_tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
</code></pre>

<p>方法我返回 <code>UITableViewCellEditingStyleNone</code> 以便我的自定义 <code>editingAccessoryView</code> 将显示。在这种方法中,我也这样做:</p>

<pre><code>self.tableView.scrollEnabled = NO;
if(self.editingPath)
{
    [ setEditing:NO animated:YES];
}

self.editingPath = indexPath;   
for (UITableViewCell *cell in )
{
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
</code></pre>

<p>这会禁用滚动,然后存储我们滑动的 <code>indexPath</code> 以供以后使用。如果您在编辑一行时滑动另一行,它将取消编辑第一行并编辑第二行,这就是苹果应用程序的行为方式。我还将所有可见单元格上的单元格 <code>selectionStyle</code> 设置为 <code>UITableViewCellSelectionStyleNone</code>。当用户在您当前编辑一个单元格时选择另一个单元格时,这会减少蓝色闪烁。</p>

<p>接下来我们需要在点击另一个单元格时关闭 <code>accessoryView</code>。为此,我们实现了这个方法:</p>

<pre><code>-(NSIndexPath *)tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.editingPath)
{
    UITableViewCell *c = ;
    ;

    self.tableView.scrollEnabled = YES;
    self.editingPath = nil;
    for (UITableViewCell *cell in )
    {
      cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    return nil;
}

return indexPath;
}
</code></pre>

<p>当有人要点击一个单元格时,如果我们正在编辑,则取消编辑该单元格并且不返回任何内容。 </p>

<p>也为</p>

<pre><code>-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
</code></pre>

<p>我返回 <code>YES</code>,以启用对我希望用户能够删除的单元格的编辑。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCell 自定义editingAccessoryView - 未正确关闭,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7699431/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7699431/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCell 自定义editingAccessoryView - 未正确关闭