UIContextualAction删除行问题。 UIContextualAction的菜单与单元格重叠

时间:2018-12-13 08:07:21

标签: ios swift uitableview uicontextualaction

我有两个UIContextualActions,应该在完成后从表中删除行。我面临以下间歇性问题: 如果我一个接一个地非常快地应用此操作,则第一个操作可以正常工作,但是当我第二次调用UIContextualAction的菜单时,它与单元格重叠。第三次,直到tableView滚动时,才可以调用UIContextualAction的菜单。 Here the short video

请在下面的代码段中找到

    //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:.destructive,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
}

0 个答案:

没有答案