我有两个 UIContextualActions 应该在完成后从表中删除行。我遇到了以下间歇性问题:如果我一个接一个地快速应用此操作,那么第一个操作可以正常工作,但是当我第二次调用 UIContextualAction 的菜单时,它会与单元格重叠。第三次,在tableView滚动之前无法调用UIContextualAction的菜单。 这里是短视频。
请在下面的代码中找到
//MARK: Swipe actions
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = self.contextualDeleteAction(forRowAtIndexPath: indexPath)
let markAsReadAction = self.contextualMarkAsReadAction(forRowAtIndexPath: indexPath)
let markAsUnreadAction = self.contextualMarkAsUnreadAction(forRowAtIndexPath: indexPath)
let swipeConfig = UISwipeActionsConfiguration(actions: [
isArchiveModeEnabled ? markAsUnreadAction : markAsReadAction,
deleteAction
])
swipeConfig.performsFirstActionWithFullSwipe = false
return swipeConfig
}
func contextualMarkAsUnreadAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .normal, title: "") { _, _, completion in
let index = indexPath.row
if let article = self.getSelectedArticleByIndex(index) {
self.articlesManager.restore(article: article)
self.refreshDataSource()
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.isEditing = false //seems like it helps to fix visual bug when the action is left on blank space
completion(true)
}
}
action.backgroundColor = UIColor(patternImage: swipeActinImages.markAsUnread)
return action
}
refreshDataSource 函数执行以下操作:
private func refreshDataSource() {
articles = isArchiveModeEnabled ? dataController.getArchivedArticles() : dataController.getUnreadArticles()
}
private func reloadTableData() {
if dataController.getActiveUser() != nil {
refreshDataSource()
introView?.isHidden = articles.count != 0 || isArchiveModeEnabled
tableView.isScrollEnabled = (introView?.isHidden)!
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
感谢任何帮助
更新
所以我解决了。我已将 UIContextualAction(style: .normal, title: "") 替换为 UIContextualAction(style: . destroy, title: "") 并且不再手动更新数据源。Completion(true) 自动对单元格和表格进行所有需要的操作。
最终代码是
func contextualMarkAsUnreadAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "") { _, _, completion in
let index = indexPath.row
if let article = self.getSelectedArticleByIndex(index) {
self.articlesManager.restore(article: article)
//self.refreshDataSource()
completion(true)
} else {
completion(false)
}
}
action.backgroundColor = UIColor(patternImage: swipeActinImages.markAsUnread)
return action
}