【问题标题】:How to update the tableview row after swipe action?滑动操作后如何更新表格视图行?
【发布时间】:2019-01-17 18:13:10
【问题描述】:

我在我的表格视图中使用滑动操作。我想在滑动操作完成后添加和删除行上的小图标。

我使用异步线程来执行此操作,但是它并没有提供我想要的平滑结果。我的代码在下面给出,任何帮助都会得到帮助。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let groceryAction = groceryToggleAction(forRowAtIndexPath: indexPath)
    let config = UISwipeActionsConfiguration(actions: [groceryAction])
    return config
}

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let consumeAction = consumeToggleAction(forRowAtIndexPath: indexPath)
    let config = UISwipeActionsConfiguration(actions: [consumeAction])
    return config
}

// MARK: Custom Methods
func groceryToggleAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
    let food = foods[indexPath.item]
    let action = UIContextualAction(style: .normal, title: "actionTitle") { (action, view, completionHandler) in

        let food = self.foods[indexPath.item]

        food.isAddedToGrocery = !food.isAddedToGrocery
        self.persistenceManager.saveContext()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            self.homeTableView.reloadRows(at: [indexPath], with: .none)
        }
        completionHandler(true)
    }
    action.image = #imageLiteral(resourceName: "shoppingCart") // İyi bir liste ikonu bul...
    action.backgroundColor = food.isAddedToGrocery ? UIColor.Palette.alizarin : UIColor.Palette.turquoise
    action.title = food.isAddedToGrocery ? "Remove" : "Add"
    return action
}

func consumeToggleAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
    let food = foods[indexPath.item]

    let action = UIContextualAction(style: .normal, title: "actionTitle") { (action, view, completionHandler) in

        food.isConsumed = !food.isConsumed
        self.persistenceManager.saveContext()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            self.homeTableView.reloadRows(at: [indexPath], with: .none)
        }
        completionHandler(true)

    }
    action.image = #imageLiteral(resourceName: "pacman")
    action.title = food.isConsumed ? "Remove": "Consumed!"
    action.backgroundColor = food.isConsumed ? UIColor.Palette.alizarin : UIColor.Palette.turquoise
    return action
}

【问题讨论】:

    标签: ios swift uitableview uicontextualaction


    【解决方案1】:

    我终于想通了。我只是从 tableview 委托中使用了下面的函数。

    func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
        print("did end editing")
        guard let indexPath = indexPath else {return}
        tableView.reloadRows(at: [indexPath], with: .none)
    }
    

    【讨论】:

    • 我也试试。感谢您的建议:-)
    【解决方案2】:

    您需要集成 UITableView 删除行,这将提供漂亮的动画,而不是笨拙的重新加载视图更新。

    tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
    

    【讨论】:

    • 我从没想过这个解决方案,听起来很合理:-)
    【解决方案3】:
    DispatchQueue.main.async
    {
        self.tableView.reloadData()
    }
    

    应该够了。

    在您的代码中,您强制应用在特定时间执行。如果您的数据是动态的,例如远程图像,它们可能会使主循环延迟 0.4 秒以上,从而导致更不稳定的更新和移动。

    【讨论】:

    • 对不起。但是我需要在滑动动作动画完成后设置图标(按钮完全滑出屏幕后)。这就是我使用 0.4 秒延迟的原因。与此同时,我的应用程序是一个完整的离线应用程序(目前......)感谢您的回答......
    • 你能把“DispatchQueue.main.async”函数放在完成处理程序中吗?
    • 我准备了下面给定的函数并将它放在complationHandler( self. updateRow())中。但是,行为仍然相同。按下滑动按钮后,该行会立即更新。 func updateRow() -> Bool { DispatchQueue.main.async { self.homeTableView.reloadData() } return true }
    猜你喜欢
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2023-03-19
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多