【问题标题】:Disable delete option for specific UITableViewCell in a UITableView禁用 UITableView 中特定 UITableViewCell 的删除选项
【发布时间】:2021-10-31 00:28:42
【问题描述】:

对于 UITableViews,我添加了三个不同的单元格

如何为特定单元格启用删除选项

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    switch dataSource[indexpath.section].menu {
        case  "Attachment":
            if editingStyle == AttachmentTableViewCell.EditingStyle.delete {
                attachmentList.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            }
            break
        default:
            break

    }
}

在这种情况下,它也会显示其他单元格的删除选项。 如何在滑动到其他 UITableViewCell 时停止显示删除选项。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    为不应显示删除选项的索引路径实现tableView(_:editingStyleForRowAt:) 并返回none

    【讨论】:

    • 是的,我的错误未能添加它。谢谢你瓦迪安
    【解决方案2】:

    这对我有用。

    UITableViewController 覆盖这个函数 或实现它UITableViewDelegate

    override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    

    索引路径是你想要一个动作返回一个UISwipeActionsConfiguration否则返回nil。

    喜欢这个

    override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
             
        // return nil on specific rows
        guard indexPath.row != 1 else {
            return nil
        }
            
        let contexualAction = UIContextualAction(style: .normal, title: "Action") { _, _, _ in
            // Do Action
        }
        let swipeAction = UISwipeActionsConfiguration(actions: [contexualAction])
    
        return swipeAction
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2017-04-07
      相关资源
      最近更新 更多