【发布时间】: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