UITableViewCell左滑时自定义编辑视图。使用Objective-C或Swift实现。

81

如何在 iOS7 UITableView 中使用 Objective C 制作类似 Evernote 或 Apple Reminders 应用程序的自定义编辑视图,实现左滑功能。

我尝试设置自定义的 editingAccessoryView,但没有成功。

Evernote 编辑视图:

enter image description here Reminders 编辑视图:

enter image description here

我的当前代码是

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"delete");
    }
}

我已经尝试使用以下方式解决问题:(UITableViewController.h)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //make cell

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [view setBackgroundColor:[UIColor greenColor]];
    //add Buttons to view

    cell.editingAccessoryView = view;

    return cell;
}

同样适用于:(UITableViewCell)

- (void)willTransitionToState:(UITableViewCellStateMask)state;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
- (UIView*)editingAccessoryView;

我们能否固定侧滑按钮的高度?例如:我的单元格高度为150,我只想显示50.0f的按钮。这可行吗? - Suhas Arvind Patil
13个回答

0

0

在表视图中的自定义单元格上创建一个视图,并将PanGestureRecognizer应用于单元格上的视图。将按钮添加到自定义单元格中,当您滑动自定义单元格上的视图时,自定义单元格上的按钮将可见。

 UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    recognizer.delegate = self;
    [YourView addGestureRecognizer:recognizer];

在该方法中处理视图上的平移

 if (recognizer.state == UIGestureRecognizerStateBegan) {
    // if the gesture has just started, record the current centre location
    _originalCenter = vwCell.center;
}

// 2
if (recognizer.state == UIGestureRecognizerStateChanged) {
    // translate the center
    CGPoint translation = [recognizer translationInView:self];
    vwCell.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
    // determine whether the item has been dragged far enough to initiate  / complete
    _OnDragRelease = vwCell.frame.origin.x < -vwCell.frame.size.width / 2;

}

// 3
if (recognizer.state == UIGestureRecognizerStateEnded) {
    // the frame this cell would have had before being dragged
    CGPoint translation = [recognizer translationInView:self];

    if (_originalCenter.x+translation.x<22) {
          vwCell.center = CGPointMake(22, _originalCenter.y);
        IsvwRelease=YES;

    }
    CGRect originalFrame = CGRectMake(0, vwCell.frame.origin.y,
                                      vwCell.bounds.size.width, vwCell.bounds.size.height);
    if (!_deleteOnDragRelease) {
        // if the item is not being dragged far enough , snap back to the original location
        [UIView animateWithDuration:0.2
                         animations:^{
                             vwCell.frame = originalFrame;
                         }
         ];
    }
}

你有例子吗? - Maulik shah

0
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        
        // your code...
        
    }];
    delete.image  = [UIImage systemImageNamed:@"trash"];
    
    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:[[NSArray alloc] initWithObjects:delete, nil]];
    
    return actions;
}

如何在向右滑动时自动触发“删除”操作? - ekashking
你应该使用 -(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath。关于UIContextualAction操作,请参考:https://dev59.com/Om035IYBdhLWcg3wNdLg - Nikodem
这已经是你的示例了。而且你的参考与我的问题无关:如何在滑动过程结束时触发“删除”操作(无需点击按钮)。 - ekashking
我了解。请看这里:https://developer.apple.com/documentation/uikit/uiswipeactionsconfiguration/2902361-performsfirstactionwithfullswipe?language=objc - Nikodem
我来重新表述一下。如何通过只滑动屏幕的三分之一来触发“删除”或任何类似WhatsApp“滑动回复消息”的操作并触发动作? - ekashking
抱歉,我无法理解您的意思。请附上问题截图并将链接粘贴在此处以提供更详细的信息。 - Nikodem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接