【问题标题】:Tableviewcells not selectable in tableview edit mode在表格视图编辑模式下无法选择表格视图单元格
【发布时间】:2015-11-27 02:25:11
【问题描述】:

我有一个包含 4 种不同类型单元格的表格视图单元格。我只需要编辑两种类型的单元格。所以我在两个单元格中添加了一个长按手势识别器。现在我需要删除这两种单元格。所以在长按手势识别器功能中,我添加了一个带有删除按钮的 UIMenuController 作为 UIMenuItem。现在在 Delete 函数中,我将 tableview 设置为可编辑模式。

- (void)Delete:(id)sender {
    NSLog(@"\n Delete Selected \n");

    [mTableView setEditing:YES animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)addLongPressGestureRecognizerForCell:(UITableViewCell *)tableViewCell{

    UILongPressGestureRecognizer *lLongPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureFunction:)];
    lLongPressGesture.minimumPressDuration = 0.3;
    lLongPressGesture.delegate = self;
    [tableViewCell addGestureRecognizer:lLongPressGesture];
//  [lLongPressGesture setCancelsTouchesInView:YES];
}

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{

    mTableView.allowsSelectionDuringEditing = YES;

    UILongPressGestureRecognizer *lLongPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureFunction:)];

    UITableViewCell *lTableViewCell = [tableView cellForRowAtIndexPath:indexPath];

    [lTableViewCell removeGestureRecognizer:lLongPressGesture];
}

现在当表格处于可编辑模式时,我无法选择两种类型的表格视图单元格。委托方法 willBeginEditingRowAtIndexPath 也被 称为

处于可编辑模式的 tableView 左侧的按钮没有被点击。我只需要在编辑模式下点击左侧的按钮,而不是整个单元格。

我应该删除可编辑模式下单元格的手势识别器吗?

我应该启用单元格的选择吗?

我已经尝试了所有选项,但没有运气。我已经没有这个实现的选项了,但是唉!!!。有人可以解释一下为了在 tableView 的编辑模式下可选择单元格而必须实施的任何更改吗?

以下是长按手势功能的代码

- (void)longPressGestureFunction:(UILongPressGestureRecognizer *)recognizer
{
    UITableViewCell *lTableViewCell = (UITableViewCell *)recognizer.view;
    [lTableViewCell becomeFirstResponder];
    UITextView *lMessageTextView = [lTableViewCell viewWithTag:103];

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        UIMenuItem *MenuDelete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(Delete:)];
        UIMenuItem *MenuForward = [[UIMenuItem alloc] initWithTitle:@"Forward" action:@selector(Forward:)];
        UIMenuItem *MenuAddToContacts = [[UIMenuItem alloc] initWithTitle:@"Add To Contacts" action:@selector(addToContacts:)];

        mSharedMenu = [UIMenuController sharedMenuController];
        [mSharedMenu setMenuItems:[NSArray arrayWithObjects:MenuDelete, MenuForward, MenuAddToContacts, nil]];

        /*Change the position of the target rect based on Sending messages or Receiving messages*/
        if ([lTableViewCell.reuseIdentifier isEqualToString:@"SendingChatCellIdentifier"]) {

            [mSharedMenu setTargetRect:lMessageTextView.frame inView:lMessageTextView.superview];

        }else if ([lTableViewCell.reuseIdentifier isEqualToString:@"ReceivingChatCellIdentifier"]){

            UILabel *senderNameLabel = [lTableViewCell viewWithTag:100];

            [mSharedMenu setTargetRect:senderNameLabel.frame inView:senderNameLabel.superview];
        }

        [mSharedMenu setMenuVisible:YES animated:YES];
  }

}

【问题讨论】:

    标签: ios uitableview edit-tableview


    【解决方案1】:

    添加长按手势是要求还是您需要?因为您可以在 didSelectRowAtIndexPath 中检测到点击并检查您的单元格类型,否则在 Uitableview 单元格中添加 uiview 并在该 uiview 中添加长按手势。

    【讨论】:

    • 这不是必需的。它是一种需要。我需要为单元格提供 2 到 3 个选项,例如删除、转发和复制。所以我想我可以用 UIMenuItems 作为这些选项的长按手势。
    • 尝试在表格单元格中添加 UIView 作为子视图并在该视图中添加手势。
    • 它将如何帮助我的事业?我希望在编辑模式下选择我的单元格。在这里添加 TableViewCell 中的 UIView 作为子视图有什么帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多