【发布时间】:2021-02-14 11:23:32
【问题描述】:
当我使用 SwipeTableViewCell 从列表中删除项目时,表格视图不会更新。 滑动有效,我也可以点击单元格后面的删除按钮,但是一旦点击它,它只会隐藏删除按钮,但不会更新 tableview。 这就是我的代码的样子:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return carList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_id", for: indexPath) as! CarCell
(cell as SwipeTableViewCell).delegate = self
let car = carList[indexPath.row]
cell.configure(with: car)
return cell
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.carList.remove(at: indexPath.row)
}
deleteAction.hidesWhenSelected = true
return [deleteAction]
}
当我调用 tableview 的 reloadData 时它可以工作,但我猜这不是正确的方式。以下 sn-p 代码有效:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.carList.remove(at: indexPath.row)
self.tableView.reloadData() // <-- this works! But without animation
}
deleteAction.hidesWhenSelected = true
return [deleteAction]
}
【问题讨论】:
标签: ios swift uitableview