UITableViewCell自定义editAccessoryView - 没有正确解雇 [英] UITableViewCell custom editingAccessoryView - not properly dismissed

查看:141
本文介绍了UITableViewCell自定义editAccessoryView - 没有正确解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了自定义编辑附件视图,如我对问题的回答中所述。在大多数情况下它工作得很好,但我注意到它有一个小问题。

I have implemented a custom editing accessory view as described in my answer to this question. For the most part it works very well, but I have noticed a small problem with it.

当我在表格视图中滚动或选择另一行时,我的自定义编辑附件不会被解除。使用标准编辑附件(删除按钮),可以捕获表格上任何其他位置的触摸并用于删除删除附件视图 - 您可以在内置的Notes应用程序中自行查看,例如,或在任何其他位置标准编辑附件视图。

When I scroll or select another row in the table view, my custom editing accessory is not dismissed. With the standard editing accessory (the delete button), a touch anywhere else on the table is captured and used to remove the delete accessory view - you can see this yourself on the built in Notes application, for instance, or in any other place with a standard editing accessory view.

这一定是因为我在刷卡到删除模式时返回 UITableViewEditingStyleNone 。但是,如果我返回任何其他模式,则不会显示我的自定义编辑附件。

This must be because I am returning UITableViewEditingStyleNone when I am in swipe-to-delete mode. However, if I return any other mode then my custom editing accessory is not displayed.

如何恢复标准编辑样式的功能,在桌面视图的任何位置触摸都会消除编辑附件?

How can I get back the functionality of the standard editing style, where a touch anywhere on the table view dismisses the editing accessory?

单元格不是子类,但它是从具有自定义布局的nib文件加载的。编辑附件视图是nib文件的一部分,通过editingAccessoryView插座连接。

The cell is not subclassed, but it is loaded from a nib file with a custom layout. The editing accessory view is part of the nib file and connected via the editingAccessoryView outlet.

我已经设法通过存储滑动到编辑行的索引路径并将该单元格设置为编辑模式(如果另一行是选择或滚动从表开始。但是,我想要正确地做到这一点。

I have managed to halfway achieve the effect I want by storing the index path of a swipe-to-edited row and setting that cell out of editing mode if another row is selected or scrolling begins on the table. However, I'd like to do it properly.

推荐答案

我能够解决这个问题,但遗憾的是它需要额外的工作,并不像设置几个属性一样简单。

I was able to solve this, but sadly it requires additional legwork and isnt just as simple as setting a couple properties.

在我的

- (UITableViewCellEditingStyle)tableView:(UITableView *)_tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

方法我返回 UITableViewCellEditingStyleNone 这样我的自定义 editingAccessoryView 就会显示出来。在这个方法中我也这样做:

method I return UITableViewCellEditingStyleNone so that my custom editingAccessoryView will show up. In this method I also do this:

self.tableView.scrollEnabled = NO;
if(self.editingPath)
{
    [[tableView cellForRowAtIndexPath:editingPath] setEditing:NO animated:YES];
}

self.editingPath = indexPath;    
for (UITableViewCell *cell in [tableView visibleCells])
{
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

这会禁用滚动,然后存储 indexPath 我们刷了以后再使用。如果您在编辑行时滑动另一行,则会在第一行无效并编辑第二行,这就是苹果应用的行为方式。我还将所有可见单元格上的单元格 selectionStyle 设置为 UITableViewCellSelectionStyleNone 。当用户在当前编辑的单元格中选择另一个单元格时,这会减少蓝色闪烁。

This disables scrolling, then stores the indexPath we swiped on for later use. If you swipe another row, while editing a row, it will unedit the first row and edit the second row, which is how apple apps behave. I also set the cell selectionStyle on all visible cells to UITableViewCellSelectionStyleNone. This reduces the blue flicker when the user selects another cell while your currently editing one.

接下来我们需要关闭 accessoryView 轻敲另一个小区。为此,我们实现了这个方法:

Next we need to dismiss the accessoryView when another cell is tapped. To do that we implement this method:

-(NSIndexPath *)tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.editingPath)
{
    UITableViewCell *c = [tableView cellForRowAtIndexPath:self.editingPath];
    [c setEditing:NO animated:YES];

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

    return nil;
}

return indexPath;
}

当有人即将点击一个单元格时,这是做什么的,如果我们正在编辑然后无限制该单元格并且不返回任何内容。

What this does is when someone is about to click on a cell, if we are editing then unedit that cell and return nothing.

也适用于

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

我返回,启用我希望用户能够删除的单元格的编辑。

I return YES, to enable editing on the cells I want the user to be able to delete.

这篇关于UITableViewCell自定义editAccessoryView - 没有正确解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆