gpt4 book ai didi

ios - 如何在滑动操作后更新 tableview 行?

转载 作者:行者123 更新时间:2023-11-28 05:47:08 40 4
gpt4 key购买 nike

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

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

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
}

最佳答案

我终于明白了。我只是使用了来自 tableview delegate 的以下函数。

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

关于ios - 如何在滑动操作后更新 tableview 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54241907/

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