【问题标题】:Implement a tablecell fade delete实现表格单元淡入淡出删除
【发布时间】:2017-06-17 10:45:27
【问题描述】:

我正在关注this answer 以获取那个漂亮的褪色已删除单元格。目前,当我滑动删除时,它是尖锐的,突然的和丑陋的。删除单元格时,其他应用程序会很好地褪色。

在我的应用中,单元格并没有真正被删除,数据被删除了:

Swift 3,功能编辑风格:

let tableSection = sections[sortedSections[indexPath.section]]
let tableItem = tableSection![indexPath.row]

// Removes item from array:
self.incomesDictionary.removeValue(forKey: tableItem.incomeId)

// Reloading the table data:
self.tableView.reloadData()

当我在...reloadData() 上方添加self.tableView.deleteRows(at: [indexPath], with: .fade) 时出现错误:

以 NSException 类型的未捕获异常终止

如果我用tableSection?.remove(at: indexPath.row) 更新函数,它仍然会崩溃。获得这种平滑效果的任何提示?

编辑:

我有一个以编程方式分组的单元格,添加 .fade 后出现的错误是:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用,原因:“尝试从第 0 节中删除第 0 行,但更新前只有 0 节”

【问题讨论】:

  • 如果您删除带有tableView.deleteRows... 的行,则以后不能再调用reloadData(),只有deleteRows... 执行动画。
  • @vadian ????但错误出现在“deleteRows...” 好的,我将删除重新加载,看看会发生什么

标签: swift uitableview swift3


【解决方案1】:

Swift 5.0

https://www.hackingwithswift.com/example-code/uikit/how-to-swipe-to-delete-uitableviewcells

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            self.myArray.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }

    }

【讨论】:

    【解决方案2】:

    试试这个:

    self.tableView.beginUpdates()
    self.tableView.deleteRows(at: [indexPath], with: .fade)
    self.tableView.endUpdates()
    

    这将为您的动画准备 tableView

    【讨论】:

    • 相同 :( 我不确定自己发生了什么。
    • 这是跟踪:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 0, but there are only 0 sections before the update'
    • 您是否也从数据源中删除了该值?
    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2011-11-06
    • 2020-07-28
    • 2011-04-28
    • 2013-11-23
    • 2018-12-18
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多