2016-11-07 32 views
1

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

实例删除功能,我说的是: 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 

感谢。

+0

将代码也在这里。 – KKRocks

+0

你想看什么代码?没有具体的东西。它是一个默认的UITableView。 – toast

+0

想要查看您的代码以进行删除。 – KKRocks

回答

0

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

我试图在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) 
} 

当它得到一个,我重装表数据,因为我numOfRowsInSection看起来是这样的:

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