Reputation: 7
Hy guys I have a problem when deleting the cell.
First I added a custom UIButton on every cell and when the button is executed it saves item in UserDefaults and it shows as painted heart img I fetch saved item in another controller but when I delete the item UIButton image stays the same. I do not know how to put button to default state(image).
First of I created as I sad the custom UIButton
lazy var favoriteButton: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(handleTapped), for: .touchUpInside)
button.setImage(#imageLiteral(resourceName: "herzfavorie").withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
return button
}()
this btn selector "handleTapped" saves item in User Defaults and makes animation
let key = "key"
@objc func handleTapped() {
guard let product = self.product else { return }
let data = NSKeyedArchiver.archivedData(withRootObject: listOfPodcasts)
UserDefaults.standard.set(data, forKey: key)
var listOfPodcasts = Product.savedProducts()
listOfPodcasts.append(product)
self.showHeartLiked()
}
that saves the item in User Defaults and creates this effect on cell but later on when I delete the product in this func
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let removalProduct = savedProducts[indexPath.row]
let tableViewAction = UITableViewRowAction(style: .default, title: "Delete") { (_, _) in
self.savedProducts.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
Product.deleteProducts(product: removalProduct)
} after this func button should be as by default
button should be as by default
but the button stays not by default I hope I explained it well when someone know the solution how to after deleting put back the button to default state I will be grateful
Upvotes: 1
Views: 85
Reputation: 7
I fixed it , I checked if the product is favorited through index(where: ...). in cellForItemAt and after deleting the product I used Notification where I only called self.collectionView.reloadData() And it works perfectly fine
Upvotes: 0
Reputation: 535
you can manage it by the button's state. set Normal heart image for normal state and filled heart image for the Button's selected state.
Now when user clicks on button you can save the data and make button state selected so it will show the filled heart image.
also when you are deleting the particular cell, at that time you can user Notification center to fire notification or use custom delegate to inform another view controller about the changes.
Upvotes: 0
Reputation: 933
Use NotificationCenter from your list view controller to alert other view controller for change button style.
Upvotes: 0