删除收藏的单元格

时间:2018-12-25 18:56:34

标签: ios swift uitableview uicollectionview

嘿,我在删除单元格时遇到问题。

首先,我在每个单元格上添加了一个自定义UIButton,执行该按钮时,它会将项目保存在UserDefaults中,并且显示为绘心img,我在另一个控制器中获取了保存的项目,但是当我删除该项目时,UIButton图像保持不变。我不知道如何将按钮设置为默认状态(图片)。

首先我创建了自定义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
    }()

此btn选择器“ handleTapped”将项目保存为用户默认设置并制作动画

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 但是后来当我在此功能中删除产品

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)

} 默认情况下,此功能按钮之后

button should be as by default

但是默认情况下按钮不会停留在我希望我能很好地解释它,当有人知道删除后将按钮恢复为默认状态的解决方法

3 个答案:

答案 0 :(得分:0)

使用列表视图控制器中的NotificationCenter来提醒其他视图控制器更改按钮的样式。

答案 1 :(得分:0)

您可以通过按钮的状态进行管理。 将正常心脏图像设置为正常状态,并将填充的心脏图像设置为Button的选定状态。

现在,当用户单击按钮时,您可以保存数据并选择按钮状态,以便显示填充的心脏图像。

当您删除特定的单元格时,那时您还可以使用Notification Center发出通知或使用自定义委托将更改通知给另一个视图控制器。

答案 2 :(得分:0)

我修复了该问题,并通过index(where:...)检查了该产品是否受到喜爱。在cellForItemAt中,删除产品后,我使用Notification,其中我仅调用self.collectionView.reloadData() 而且效果很好

相关问题