显示UIMenuController取消选择UITableViewCell [英] Showing UIMenuController deselects UITableViewCell

查看:148
本文介绍了显示UIMenuController取消选择UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在显示UIMenuController的表格视图单元格上实现了长按手势识别器.但是当菜单显示时,相应的表格视图单元格将取消选择.在显示菜单之前,我会根据需要调用[self becomeFirstResponder].我认为此调用会取消选择单元格,但是如何在UIMenuController可见的情况下使其保持选中状态?

I've implemented a long press gesture recognizer on a table view cell that shows the UIMenuController. But when menu shows, the corresponding table view cell deselects. Before showing the menu, I call, as required, [self becomeFirstResponder]. I think that this call deselects the cell, but how to make it to stay selected while the UIMenuController is visible?

推荐答案

一种简单的实现方法是使用特定的UITableViewDelegate方法来处理UIMenuController. 但是首先,要使该单元格保持选中状态,请在您的课程中存储显示菜单的单元格的值:

A simpler way of implementing this is using the specific UITableViewDelegate methods for dealing with UIMenuController. But first, to make the cell stay selected, store the value of the cell presenting the menu in your class:

NSIndexPath                     *_editingIndexPath;

然后实现UITableViewDelegateMethods:

Then implement the UITableViewDelegateMethods:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomTableViewCell *cell = (MyCustomTableViewCell *) [_tableView cellForRowAtIndexPath:indexPath];
    _editingIndexPath = indexPath;
    cell.showingMenu = YES;

    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)) {
        return YES;
    }

    return NO;
}


- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:))
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        if (cell && [cell isKindOfClass:[MessageConversationCell class]])
        {
            [UIPasteboard generalPasteboard].string = cell.textLabel.text;
        }
    }
}

上面的代码将负责在长按后在单元格上显示复制"菜单. 现在,如果您希望在显示菜单时保持选中该单元格:

The code above will take care of showing a "copy" menu on the cell after a long press. Now, if you want the cell to stay selected while the menu is displayed:

在名为"showingMenu"的自定义单元格中添加一个@property(请注意,此答案的第一段代码中已经设置了此属性).

Add a @property in your custom cell named "showingMenu" (note that this property was already set in the first block of code in this answer).

@property (nonatomic, assign) BOOL showingMenu;

将以下方法添加(或修改,如果已经存在的话)到您的自定义单元格中.在菜单尝试取消突出显示该单元格后,这将使该单元格保持突出状态(您可以实现自己的逻辑来突出显示一个单元格,在这种情况下,将其放在if条件语句的第一分支中):

Add (or modified if already present) the following method to your custom cell. This will take care of keeping the cell highlighted after the menu tried to unhighlight it (you may implement your own logic for highlighting a cell, in that case put it in the first branch of the if conditional):

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{

    if (_showingMenu)
    {
        [super setHighlighted:YES]
    }
    else
    {
        [super setHighlighted:highlighted];
    }
}

添加要在要显示菜单时得到通知的观察者.这进入视图控制器,而不是自定义单元中:

Add an observer to be notified when the menu is going to be presented. This goes into the view controller, NOT in the custom cell:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowEditMenu:) name:UIMenuControllerDidShowMenuNotification object:nil];

在视图控制器上添加显示菜单时要调用的方法:

Add on the view controller the method to be called when the menu is displayed:

- (void)didShowEditMenu:(NSNotification *)not {
    [_tableView selectRowAtIndexPath:_editingIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    MyCustomTableViewCell *cell = (MyCustomTableViewCell*)[_conversationTableView cellForRowAtIndexPath:_editingIndexPath];
    cell.showingMenu = NO;
}

也不要忘记在不再需要观察者时将其删除:

And don't forget to remove the observer when no longer needed:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerDidShowMenuNotification object:nil];

长按一个单元格时,将显示一个菜单,并保持选中该单元格直到菜单消失,这可能是因为选择了一个选项,或者是因为用户点击了其他位置.选择邮件时,它的工作原理与Whatsapp的工作原理非常相似.

This will show a menu when a cell is long pressed, and keep the cell selected until the menu disappears, either because an option was chosen or because the user tapped somewhere else. It works pretty much like Whatsapp works when you select a message.

这篇关于显示UIMenuController取消选择UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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