热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

具有破坏性样式的UIContextualAction似乎默认删除行

如何解决《具有破坏性样式的UIContextualAction似乎默认删除行》经验,为你挑选了1个好方法。

我看到的问题是,当我创建一个UIContextualActionwith .destructive 传入truecompletionHandler,似乎有一个删除行的默认操作.

如果您从Xcode的模板创建一个新的Master-Detail应用程序并将此代码添加到MasterViewController...

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let testAction = UIContextualAction(style: .destructive, title: "Test") { (_, _, completionHandler) in
        print("test")
        completionHandler(true)
    }
    return UISwipeActionsConfiguration(actions: [testAction])
}

您要滑动的行将被删除.请注意,没有代码更新表视图.此外,模型不会更新,如果向上滚动导致重新加载行,它将重新出现.

false在这种情况下传递不会删除该行.或使用.normal样式,true也不删除行.

.destructivetrue导致默认情况下删除行.

谁能解释这种行为?为什么要删除行?



1> Kris Gellci..:

根据破坏性选项的文档:

删除数据或执行某种类型的破坏性任务的操作.

完成意味着表明行动是否成功.传递真实意味着破坏性任务是成功的,因此应该删除行.

您需要在发生破坏性操作时手动更新dataSource,否则会导致滚动以重新显示数据.您还需要告诉tableView数据已被删除.

下面是一些显示工作示例的代码:

UIContextualAction(style: .destructive, title: "Delete") { [weak self] (_, _, completion) in
    if self?.canDelete(indexPath) { // We can actually delete
        // remove the object from the data source
        self?.myData.remove(at: indexPath.row)
        // delete the row.  Without deleting the row or reloading the
        // tableview, the index will be off in future swipes
        self?.tableView?.deleteRows(at: [indexPath], with: .none)
        // Let the action know it was a success.  In this case the 
        // tableview will animate the cell removal with the swipe
        completion(true)    
    } else { // We can't delete for some reason
        // This resets the swipe state and nothing is removed from
        // the screen visually.
        completion(false)
    }         
}

然后我需要重新加载tableview或调用deleteRows,以便indexPath在下一次滑动时正确计算.

如果我有10行并且我将第5行拖动以删除,那么除非tableview重新加载或者tableview被告知要以某种方式移除行,否则之后的每一行都将关闭一行.


推荐阅读
author-avatar
手浪用户穹谷之鹰
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有