tableviewcells在tableview编辑模式

时间:2015-11-26 18:04:27

标签: ios uitableview edit-tableview

我有一个包含4种不同类型细胞的tableview细胞。我只需要编辑两种类型的单元格。所以我在两个单元格中添加了一个长按手势识别器。现在我需要删除这两种细胞。所以在长按手势识别器功能中,我添加了一个带有删除按钮的UIMenuController作为UIMenuItem。现在在删除功能中,我将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];
}

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

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

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

我应该启用细胞的选择吗?

我尝试过所有选项,但没有运气。我用完了这个实现的选项但是Alas !!!有人可以解释一下在tableView的编辑模式下可以选择单元格的任何变化吗?

以下是Long Press Gesture功能的代码

- (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];
  }

}

1 个答案:

答案 0 :(得分:0)

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