ios - 滚动时Swift 3选择的按钮在 Collection View 中改变位置

标签 ios swift uicollectionview uicollectionviewcell

我正在尝试制作一个 Collection View ,其中每个单元格都包含一个图像和一个可以选择或不选择的按钮。最初一切似乎都运行良好,但是在我按下一些按钮并在 View 中移动后,一些“选定”按钮变为“未选定”,反之亦然。

我发现人们在图像切换位置时遇到问题,所以我尝试缓存我的图像。我也尝试过完全删除图像,但问题仍然存在。

这是我的收藏 View 设置:

override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imageArray.count

}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

    //cell.photo.image = imageArray[indexPath.row]

    cell.selectButton.tag = indexPath.row
    cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let length = collectionView.frame.width / 3 - 1


    return CGSize(width: length, height: length)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    //sets spacing between images
    return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    //sets spacing between images
    return 1.0
}

这是我的 buttonPressed 函数:

func buttonPressed(sender: UIButton) {

    if sender.isSelected == true{
        sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
        sender.isSelected = false
    }else{
        sender.setTitleColor(UIColor.red, for:UIControlState.normal)
        sender.isSelected = true
    }
}

如何解决?

最佳答案

您需要做的就是在 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

中每次创建单元格时检查按钮条件

获取一个数组并将所有选定的索引保存在其中。

 var mArray = [Int]()


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

        //cell.photo.image = imageArray[indexPath.row]

        cell.selectButton.tag = indexPath.row
        cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

//check if indexpath.row is selected
    if mArray.contains(indexPath.row){
                cell.selectButton.setTitleColor(UIColor.red, for:UIControlState.normal)
                cell.selectButton.isSelected = true
    }else{
                cell.selectButton.setTitleColor(UIColor.blue, for:UIControlState.normal)
                cell.selectButton.isSelected = false
    }


        return cell

    }


func buttonPressed(sender: UIButton) {

    if sender.isSelected == true{
        mArray.removeAtIndex(sender.tag)
        sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
        sender.isSelected = false
    }else{
        mArray.append = sender.tag
        sender.setTitleColor(UIColor.red, for:UIControlState.normal)
        sender.isSelected = true
    }
}

关于ios - 滚动时Swift 3选择的按钮在 Collection View 中改变位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019264/

相关文章:

android - CN1模拟器默认屏幕大小

ios - iPhone 不支持任何应用程序架构。您可以将 arm64e 架构添加到应用程序的架构build设置中

ios - Swift 不允许我有 2 个同名选择器

swift - 使用 SwiftUI 时尝试在 View 上显示工作表时应用程序崩溃

ios - 使用 iOS 7.1 从 UICollectionViewController 进行推送转换时 UINavigationBar 后面的深色背景

ios - 在格式化时间的开头删除 0

ios - 单击/点击该行后才会显示图像

ios - 如何读取父节点名称而不从 firebase For ios 获取值

ios - UICollectionView:重新加载数据,而不删除单元内动画

ios - UICollectionView scrollToItemAtIndexPath : and get fresh cell position