【问题标题】:How to implement favorite button in tableView cell?如何在 tableView 单元格中实现收藏按钮?
【发布时间】:2021-04-09 08:46:36
【问题描述】:

我需要实现一个保存按钮。当用户点击按钮时,它必须被填充,默认情况下它是未填充的。但是当我运行我的应用程序时,tableView 单元格中的一些按钮已经填满like that

这是来自 TableViewCell 的代码

var isFavorite: Bool = false

private let addToFavorites: UIButton = {
    let button = UIButton(type: .custom)
    button.setImage(UIImage(systemName: "heart"), for: .normal)
    button.tintColor = UIColor.white.withAlphaComponent(0.85)
    button.contentVerticalAlignment = .fill
    button.contentHorizontalAlignment = .fill
    return button
}()

override func awakeFromNib() {
        super.awakeFromNib()
    
    setFavoriteButtonUI()
    addToFavorites.addTarget(self, action: #selector(markFavorite), for: .touchUpInside)
}

@objc func markFavorite() {
    setFavoriteButtonImage()
}

private func setFavoriteButtonImage() {
        isFavorite = !isFavorite
        let imgName = isFavorite ? "heart" : "heart.fill"
        let favoriteButtonImage = UIImage(systemName: imgName)
        self.addToFavorites.setImage(favoriteButtonImage, for: .normal)
    }

private func setFavoriteButtonUI() {

    addToFavorites.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(addToFavorites)
    
    addToFavorites.topAnchor.constraint(equalTo: filmImgView.topAnchor, constant: 40).isActive = true
    addToFavorites.trailingAnchor.constraint(equalTo: filmImgView.trailingAnchor, constant: -20).isActive = true
    addToFavorites.heightAnchor.constraint(equalToConstant: 30).isActive = true
    addToFavorites.widthAnchor.constraint(equalToConstant: 40).isActive = true
}

在我添加的 cellForRowAt indexPath 方法中

tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.top)

【问题讨论】:

    标签: swift uitableview uibutton


    【解决方案1】:

    这是iOS初学者的经典问题。

    TableViewCell 具有重用对象池的机制。

    当某个充满心的单元格滑出屏幕时,该单元格进入重用对象池。

    新单元格出现在屏幕上,可能是创建的,也可能是从重用对象池中提取的。


    简单的解决方案是Mark & Config

    只是为了将cell状态数据保持在cell范围之外,我们通常在controller上有状态数据源。

    @objc func markFavorite()更改状态数据源,然后table重新加载数据。

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell ,配置小区心脏状态。


    在控制器中:

    var selected = [Int]()
    
    
    func select(index idx: Int){
       selected.append(idx)
       table.reloadData()
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        // ...
        cell.proxy = self
        cell.config(willShow: selected.contains(indexPath.row), idx: indexPath.row)
    
    }
    

    在单元格中:

    var proxy: XXXProxy? // learn sth proxy yourself
    
    var idx: Int?
    
    func config(willShow toShow: Bool, idx index: Int){
         idx = index
         btn.isHidden = !toShow
    }
    
    // add the rest logic yourself
    

    【讨论】:

    • 感谢您尝试我的帮助。你的意思是我应该在 cellForRow 方法中调用 .addTarget 并在 tableView 控制器中调用选择器?我试过这个: cell.addToFavorites.addTarget(self, action: #selector(MoviesViewController.markFavorite), for: .touchUpInside) 并在选择器中:@objc func markFavorite() { isFavorite = !isFavorite let imgName = isFavorite ? "heart" : "heart.fill" let favoriteButtonImage = UIImage(systemName: imgName) cell.addToFavorites.setImage(favoriteButtonImage, for: .normal) tableView.reloadData() }
    • 现在所有按钮都未填充但如果我点击按钮则无法填充:(
    • 刷新。自己添加其余逻辑
    • 这里有你可以学习的东西stackoverflow.com/questions/65028109/…
    • 在这里你可以得到一些想法。 stackoverflow.com/questions/64328299/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2022-06-14
    相关资源
    最近更新 更多