其本质是将心的图像从空替换为填充,这取决于是否从leadingSwipeActionsConfigurationForRowAt中选择了元素(到目前为止结果只是改变了颜色)这里是代码本身
`
func favoriteActions(at indexPath: IndexPath) -> UIContextualAction {
var object = objects[indexPath.row]
let action = UIContextualAction(style: .normal, title: "Favorite") { (action, view, completion) in
object.isFavorite = !object.isFavorite
self.objects[indexPath.row] = object
completion(true)
}
action.backgroundColor = object.isFavorite ? .systemGray : .systemPurple
action.image = UIImage(systemName: "suit.heart.fill")
return action
}
`
答案原来很简单,但是到现在我还写不下三元运算符