Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在tableView单元格中实现收藏夹按钮?_Swift_Uitableview_Uibutton - Fatal编程技术网

Swift 如何在tableView单元格中实现收藏夹按钮?

Swift 如何在tableView单元格中实现收藏夹按钮?,swift,uitableview,uibutton,Swift,Uitableview,Uibutton,我需要实现一个保存按钮。当用户点击按钮时,它必须被填充,默认情况下它是未填充的。但当我运行我的应用程序时,tableView单元格中的一些按钮已经被填满了 以下是TableViewCell中的代码 var isFavorite: Bool = false private let addToFavorites: UIButton = { let button = UIButton(type: .custom) button.setImage(UIImage(systemName:

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

以下是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中,我添加了一个XPath方法

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

对于iOS初学者来说,这是一个经典问题

TableViewCell
具有重用对象池的机制

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

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


简单的解决方案是
Mark&Config

为了维护单元范围外的单元状态数据,我们通常在控制器上有状态数据源

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

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:#选择器(MoviesViewController.markFavorite),for:.touchupin)和选择器:@objc func markFavorite(){isFavorite=!isFavorite let imgName=isFavorite?:“heart.fill”let favoriteButonimage=UIImage(系统名:imgName)cell.addToFavorites.setImage(FavoriteButonimage,for:.normal)tableView.reloadData()}现在所有按钮都未填充,但如果我点击按钮,它将无法填充:(刷新。添加其余逻辑您自己这里有一些您可以学习的内容您可以了解一些想法。