简体   繁体   English

在编辑模式下删除表格行上的删除按钮⛔️

[英]remove the delete button ⛔️ on table rows in edit mode

I am trying to learn Swift, but there is a problem in my project that drives me nuts.我正在尝试学习 Swift,但我的项目中有一个问题让我抓狂。 I have a working list of data in a ViewController fed by parse.com.我在 parse.com 提供的 ViewController 中有一个工作数据列表。 I managed to implement a swipe-feature that reveals buttons for both deleting and editing.我设法实现了一个滑动功能,可以显示用于删除和编辑的按钮。 That is working fine.那工作正常。 Now I want the user to be able to reorder the cells.现在我希望用户能够重新排序单元格。 So I successfully implemented a button to put the table into editing-mode.所以我成功地实现了一个按钮,将表格置于编辑模式。 My 2 problems with that are:我的两个问题是:

  1. When I enter edit-mode I just want to be able to reorder the cells since editing and deleting is done via swipe (via "tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath)". How can I achive that the user doesn't see the 2 buttons for deleting and editing when in editing-mode and touching the delete-circle that is provided automatically?当我进入编辑模式时,我只想能够重新排序单元格,因为编辑和删除是通过滑动完成的(通过“tableView(tableView:UITableView,editActionsForRowAtIndexPath indexPath:NSIndexPath)”。我怎样才能做到用户没有在编辑模式下看到用于删除和编辑的 2 个按钮并触摸自动提供的删除圆圈?

  2. Is it possible to remove the delete-circle altogether?是否可以完全删除删除圈? Using "UITableViewCellEditingStyle.None" also disables the swipe-functionality.使用“UITableViewCellEditingStyle.None”也会禁用滑动功能。

Thanks in advance!提前致谢!

To avoid the round red delete button that appears when you put set UITableView isEditing to true at the left and does nothing when you click it, the minimum that worked for me was this (Swift 4, iOS 11)为了避免在左侧将 set UITableView isEditing 设置为 true 时出现的圆形红色删除按钮,并且在单击它时不执行任何操作,对我有用的最小值是(Swift 4,iOS 11)

// Avoid the round red delete button on the left of the cell: // 避免单元格左侧的圆形红色删除按钮:

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}


func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}

I also have these functions, which probably interact:我也有这些功能,它们可能会相互作用:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return savedTempTable.isEditing
}

// Including this function in the delegate enable left-swipe deleting
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    if editingStyle == .delete {
        savedConversions.remove(at: indexPath.row)
    }
}


// Including this function enables reordering
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath)
{
    let elem = savedConversions.remove(at: sourceIndexPath.row)
    savedConversions.insert(elem, at: destinationIndexPath.row)
}

Although people can delete a row through swipe the delete-button in editing mode should not be removed.虽然人们可以通过滑动来删除一行,但编辑模式下的删除按钮不应被删除。 People may not know about the swipe gesture and by removing the delete button (which they already expect in editing mode) the app becomes more difficult to use.人们可能不知道滑动手势,通过删除删除按钮(他们在编辑模式下已经期望),应用程序变得更难使用。

If you really want to remove the delete button then you have to implement the delegate method tableView(_:editingStyleForRowAtIndexPath:) .如果你真的想删除删除按钮,那么你必须实现委托方法tableView(_:editingStyleForRowAtIndexPath:) There you can return .None while the screen is in editing mode and .Delete while the screen is not.在那里,你可以返回.None而屏幕在编辑模式和.Delete而屏幕是没有的。

To enable reordering you have to implement the data source methods tableView(_:canMoveRowAtIndexPath:) and tableView(_:moveRowAtIndexPath:toIndexPath:) .要启用重新排序,您必须实现数据源方法tableView(_:canMoveRowAtIndexPath:)tableView(_:moveRowAtIndexPath:toIndexPath:)

You follow this way to remove the delete Icon while editing:您可以按照这种方式在编辑时删除删除图标:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
     return UITableViewCellAccessoryNone;
 }
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}

override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM