TableView删除HTTP中的行删除Closure Swift

时间:2018-01-10 22:11:28

标签: ios swift uitableview swift4

我目前不确定如何解决这个问题。基本上我有一个tableview,我可以通过滑动删除数据。以下是受影响线的代码。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        // Delete the row from the data source

        guard let id = CastService.instance.datsSource[indexPath.row].id else {return}

        CastService.instance.deleteCast(id: id, completion: { (success) in

            if success {
                // Have the below line and that crashes. I believe this is because database has already removed the data
                tableView.deleteRows(at: [indexPath], with: .fade)

                // Added below line to see if reloading data does anything, however nothing happened, data was still there
               // self.tableview.reloadData()
            }
        })
    }
}

dataSource声明

fileprivate var dataSource = CastService.instance.datsSource

numberOfRowsInSection Code

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataSource.count
}

我目前正试图弄清楚如何解决这个问题,因此用户可以将行删除

以下崩溃错误

Interstellar Battalion[62182:2255764] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011
2018-01-10 22:13:26.879942+0000 Interstellar Battalion[62182:2255764] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f7ed12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010e934f41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f7f22f2 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010e3d5d69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   UIKit                               0x0000000110f4f209 -[UITableView _endCellAnimationsWithContext:] + 19416
    5   UIKit                               0x0000000110f6b67a -[UITableView _updateRowsAtIndexPaths:withUpdateAction:rowAnimation:usingPresentationValues:] + 1380
    6   UIKit                               0x0000000110f6b8ba -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 121
    7   Interstellar Battalion              0x000000010ddab8f2 _T022Interstellar_Battalion28MovieCastTableViewControllerC05tableF0ySo07UITableF0C_SC0iF16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttFySbcfU_ + 258
    8   Interstellar Battalion              0x000000010ddababc _T022Interstellar_Battalion28MovieCastTableViewControllerC05tableF0ySo07UITableF0C_SC0iF16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttFySbcfU_TA + 140
    9   Interstellar Battalion              0x000000010dda9604 _T022Interstellar_Battalion11CastServiceC06deleteC0ySi2id_ySbc10completiontFy9Alamofire12DataResponseVySSGcfU_ + 612
    10  Interstellar Battalion              0x000000010dda9ae2 _T022Interstellar_Battalion11CastServiceC06deleteC0ySi2id_ySbc10completiontFy9Alamofire12DataResponseVySSGcfU_TA + 66
    11  Alamofire                           0x000000010e135ff5 _T09Alamofire12DataResponseVySSGIxx_ADIxi_TR + 213
    12  Alamofire                           0x000000010e136092 _T09Alamofire12DataResponseVySSGIxx_ADIxi_TRTA + 66
    13  Alamofire                           0x000000010e131809 _T09Alamofire11DataRequestC8responseACXDSo13DispatchQueueCSg5queue_x0D10SerializeryAA0B8ResponseVy16SerializedObjectQzGc17completionHandlertAA0biH8ProtocolRzlFyycfU_yycfU_ + 905
    14  Alamofire                           0x000000010e13a07e _T09Alamofire11DataRequestC8responseACXDSo13DispatchQueueCSg5queue_x0D10SerializeryAA0B8ResponseVy16SerializedObjectQzGc17completionHandlertAA0biH8ProtocolRzlFyycfU_yycfU_TA + 110
    15  Alamofire                           0x000000010e0e87f9 _T0Ix_IyB_TR + 41
    16  libdispatch.dylib                   0x0000000114c122f7 _dispatch_call_block_and_release + 12
    17  libdispatch.dylib                   0x0000000114c1333d _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x0000000114c1e5f9 _dispatch_main_queue_callback_4CF + 628
    19  CoreFoundation                      0x000000010f7afe39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    20  CoreFoundation                      0x000000010f774462 __CFRunLoopRun + 2402
    21  CoreFoundation                      0x000000010f773889 CFRunLoopRunSpecific + 409
    22  GraphicsServices                    0x00000001183469c6 GSEventRunModal + 62
    23  UIKit                               0x0000000110e2b5d6 UIApplicationMain + 159
    24  Interstellar Battalion              0x000000010dda6c37 main + 55
    25  libdyld.dylib                       0x0000000114c8fd81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:5)

您收到此次崩溃是因为当您在UITableView中添加或删除行时,还需要在数据源中添加或删除相同数量的项目。

例如,您可以将功能更改为:

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