gpt4 book ai didi

ios - 在tableview中删除一行

转载 作者:行者123 更新时间:2023-11-28 09:50:51 25 4
gpt4 key购买 nike

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

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()
}
})
}
}

数据源声明

fileprivate var dataSource = CastService.instance.datsSource

numberOfRowsInSection 代码

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:20112018-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

最佳答案

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

例如,您可以将函数更改为:

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

关于ios - 在tableview中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48196939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com