【问题标题】:Swipe to Delete being tapped twice滑动以删除被轻按两次
【发布时间】:2016-11-07 09:39:31
【问题描述】:

如何阻止滑动删除功能在我的表格视图上被点按两次?它通过尝试两次删除数据或访问已删除的数据而导致崩溃。

我正在谈论的滑动删除功能的示例: http://media.tumblr.com/25eb2fcf7aa2cd84f5cb27c54ca1ad5b/tumblr_inline_muotqqEYrY1qzumo9.jpg

与滑动删除功能相关的所有代码: http://pastebin.com/5VEbPvEC

这是要删除的代码:

//HTTP GET request to delete from server
//Then in:
DispatchQueue.main.async {
...
self.leaderboardData.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}

编辑:我也尝试禁用用户交互:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    cell?.isUserInteractionEnabled = false

删除事件还是被触发了两次。

编辑2:

当我点击两次时,我会崩溃:

libswiftFoundation.dylib`static Foundation.IndexPath._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath:
    0x100c72914 <+0>:  stp    x20, x19, [sp, #-32]!
    0x100c72918 <+4>:  stp    x29, x30, [sp, #16]
    0x100c7291c <+8>:  add    x29, sp, #16              ; =16 
    0x100c72920 <+12>: mov    x19, x0
    0x100c72924 <+16>: cbz    x19, 0x100c7294c          ; <+56>
    0x100c72928 <+20>: mov    x0, x19
    0x100c7292c <+24>: bl     0x100c9ae14               ; function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Foundation.IndexPath.init (nsIndexPath : __ObjC.NSIndexPath) -> Foundation.IndexPath
    0x100c72930 <+28>: mov    x20, x0
    0x100c72934 <+32>: mov    x0, x19
    0x100c72938 <+36>: bl     0x100cdc2e4               ; symbol stub for: objc_release
    0x100c7293c <+40>: mov    x0, x20
    0x100c72940 <+44>: ldp    x29, x30, [sp, #16]
    0x100c72944 <+48>: ldp    x20, x19, [sp], #32
    0x100c72948 <+52>: ret    
->  0x100c7294c <+56>: brk    #0x1

或者我看到了其中的一些:

/api/leaderboards/delete/132 returned 500.

500 表示删除记录失败,因为没有记录。

这只发生在我混合出现的删除按钮时。

编辑 3:

我对我的代码做了一点改动,现在捣碎删除按钮不会导致崩溃,但是每当我删除最后一行时,我就会崩溃。

代码: http://pastebin.com/57hMZQiJ

在以下行触发断点:

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

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UITableView.m:1610

谢谢。

【问题讨论】:

  • 把代码也放在这里。
  • 你想看什么代码?没有什么具体的。它是一个默认的 UITableView。
  • 想查看您的删除代码。
  • 您可以在修改数据源时禁用与屏幕的交互
  • @KKRocks 我更新了帖子。如果您想查看更多代码,请告诉我。我不知道还能放什么。谢谢。

标签: ios swift uitableview


【解决方案1】:

解决了。所以我遇到了一些问题。

我试图在didSelectRowAt 中禁用用户交互。而不是我现在做的func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

我认为这解决了“删除”按钮问题。

然后,我仅在删除最后一行时才崩溃的问题来自numRowsInSection 的错误返回值。

当数据数组为空时,我加载一个占位符单元格,为用户提供一些关于该做什么的信息。这使得应用程序尝试读取不再存在的索引。所以我通过这样做解决了它:

self.leaderboardData.remove(at: indexPath.row)
if self.tableView.numberOfRows(inSection: indexPath.section) == 1 {
    self.tableView.reloadData()
}
else {
    self.tableView.deleteRows(at: [indexPath], with: .fade)
}

当它达到 1 时,我重新加载表数据,因为我的 numOfRowsInSection 看起来像这样:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count = 0
    if leaderboardData.count < 1 {
        count = 1
    }
    else {
        count = leaderboardData.count
    }
    return count
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多