根据 CollectionView 中的选择更改 ImageView

     2023-03-14     75

关键词:

【中文标题】根据 CollectionView 中的选择更改 ImageView【英文标题】:Changing ImageView Based on Selection In A CollectionView 【发布时间】:2017-08-16 21:27:05 【问题描述】:

我目前有一个 CollectionView 和 1 个可重复使用的单元格,其中填满了 ImageView。这个单元格被使用了 10 次来展示 10 种不同的动物(我提供了单元格的图片)。我在这个集合视图上方也有一个标签。每次用户按下单元格时,标签都会变为“所选动物的名称”。 我现在想要的是让单元格图像在被选中时变暗,在未选中时恢复正常。到目前为止,我知道如何将单元格的图像更改为更暗的图像手动将其切换为较暗的,但我不知道如何将其更改为正常,因为我选择了另一个。 我该如何实现?我在底部复制了我的代码:

//cell configuration
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

        //define the cell
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AnimalSelectionCell

        //animals is name of array of different types of animals
        cell.animalImage.image = UIImage(named: animals[indexPath.row])
        cell.animalImage.restorationIdentifier = animals[indexPath.row]

        return cell
    

    //choosing an animal
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)         
            let currentCell = collectionview.cellForItem(at: indexPath) as! AnimalSelectionCell
            let animal = currentCell.animalImage.restorationIdentifier! //gets what type of animal selected
            selectionLabel.text = "\(animal) selected" //changes label
            currentCell.animalImage.image = UIImage(named: animal + "Selected") //changes to darker animal image                 
        

【问题讨论】:

【参考方案1】:

您可以定义一个名为 selectedIndexpathvar 并在您的 didSelectItemAt 方法中通过选定的 indexpath 进行更改,如果 selectedIndexPath 相同,您应该将 selectedIndexpath 设置为 nil 并且在您的 cellForItemAt 方法中您只需要检查当前 indexPath 是否等于您的 selectedIndexpath 属性

var selectedIndexPath : IndexPath? //declare this

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

        //define the cell
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AnimalSelectionCell

        //animals is name of array of different types of animals
        cell.animalImage.image = UIImage(named: animals[indexPath.row])
        if(indexPath == selectedIndexPath)
            //if indexpath is selected use your darker image
            cell.animalImage.image = UIImage(named: animal + "Selected")
        
        cell.animalImage.restorationIdentifier = animals[indexPath.row]
        return cell
    

    //choosing an animal
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)         
            let currentCell = collectionview.cellForItem(at: indexPath) as! AnimalSelectionCell
            let animal = currentCell.animalImage.restorationIdentifier! //gets what type of animal selected
            selectionLabel.text = "\(animal) selected" //changes label
            var prevSelectedIndexPath : IndexPath?
            if(selectedIndexPath != nil)
                 if(selectedIndexPath == indexPath)
                     selectedIndexPath = nil
                 else
                     prevSelectedIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section)
                     selectedIndexPath = indexPath
                 
            else
                selectedIndexPath = indexPath
            

            if(prevSelectedIndexPath != nil)
                 collectionView.reloadItems(at: [indexPath,prevSelectedIndexPath])
            else
                 collectionView.reloadItems(at: [indexPath])
            
        

func clearSelection()
    if(selectedIndexPath != nil)
         let prevSelectedIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section)
         selectedIndexPath = nil
         self.collectionView.reloadItems(at: [prevSelectedIndexPath])
    

【讨论】:

这使所选动物的图像变暗,但是每当我选择另一个动物时,我之前选择的动物图像仍然变暗。我该如何解决这个问题? 我还有一个清除按钮,当我按下它时,标签变为“未选择动物”。我应该在 IBAction 代码中使用这段代码的哪一部分,这样当我按下清除按钮时,所有图像都会恢复正常? @fphelp 很高兴为您提供帮助【参考方案2】:

为此,您必须继承 UICollectionViewCell 并覆盖 isSelected 属性:

class MyCell: UICollectionViewCell 
    override var isSelected 
        willSet(bool) 
            self.backgroundColor = UIColor.gray // or whatever
        
    

【讨论】:

CollectionView -> 根据是不是登录更改流程

】CollectionView->根据是不是登录更改流程【英文标题】:CollectionView->changingflowbasedonifloggedinCollectionView->根据是否登录更改流程【发布时间】:2013-08-0100:36:40【问题描述】:我有一个使用StoryBoard、CollectionViews和NavigationControll... 查看详情

如何根据设备主题更改collectionview单元格颜色(按照我的配色方案)

】如何根据设备主题更改collectionview单元格颜色(按照我的配色方案)【英文标题】:Howtochangecollectionviewcellscolorbasedondevicetheme(followingmycolorscheme)【发布时间】:2020-10-2815:27:08【问题描述】:概述:我正在使用集合视图构建键盘... 查看详情

根据选择的collectionview单元格显示不同的tableview数据

】根据选择的collectionview单元格显示不同的tableview数据【英文标题】:Displaydifferenttableviewdatadependingonwhichcollectionviewcellisselected【发布时间】:2019-06-2722:30:02【问题描述】:Here\'sascreenshotoftheUI我的tableview上方有一个collectionview。我... 查看详情

根据 UICollectionView 内容快速移动视图

】根据UICollectionView内容快速移动视图【英文标题】:MoveaviewdependingonUICollectionViewcontentoffsideswift【发布时间】:2020-02-0717:59:40【问题描述】:您好:我正在尝试创建一个collectionView,页面顶部有一个segmentedControl,它会根据collection... 查看详情

根据 UICollectionView 中的图像动态更改单元格大小

】根据UICollectionView中的图像动态更改单元格大小【英文标题】:DynamicallychangecellsizeaccordingtoimageinaUICollectionView【发布时间】:2014-02-0310:37:58【问题描述】:我在水平集合视图中显示从服务器接收的动态图像。当我设置collectionview... 查看详情

如何根据另一个pickervew更改pickerview中的选择?

】如何根据另一个pickervew更改pickerview中的选择?【英文标题】:HowcanIchangethechoicesinapickerviewbasedonanotherpickervew?【发布时间】:2020-02-1413:00:51【问题描述】:Pictureofcurrentversion我目前有两个选择器视图。一个是国家选择器视图(美... 查看详情

根据下拉菜单中的选择更改 div 的内容

】根据下拉菜单中的选择更改div的内容【英文标题】:Changethecontentofadivbasedonselectionfromdropdownmenu【发布时间】:2011-09-0402:29:54【问题描述】:有没有一种简单的方法,使用JavaScript,根据用户从下拉菜单中的选择动态显示/隐藏&... 查看详情

根据选项选择更改引导滑块中的值

】根据选项选择更改引导滑块中的值【英文标题】:Changevalueinboostrapsliderbasedonoptionselect【发布时间】:2017-10-0517:36:18【问题描述】:我正在尝试在选择选项时更改引导滑块的限制和范围值。我也尝试了一些东西,但没有成功。... 查看详情

如何根据用户选择在 CollectionView 标题下方显示不同的单元格?

】如何根据用户选择在CollectionView标题下方显示不同的单元格?【英文标题】:HowtodisplaydifferentcellsbelowCollectionViewheaderdependingonusersselection?【发布时间】:2017-05-2106:53:40【问题描述】:编辑:我正在尝试重新创建instagram/twitter个人... 查看详情

WPF:尝试根据组合框中的选择更改可见性

】WPF:尝试根据组合框中的选择更改可见性【英文标题】:WPF:Tryingtochangevisibilitybasedonselectionincombobox【发布时间】:2020-04-0723:07:26【问题描述】:我想根据组合框中的选择显示不同的堆栈面板。想法是折叠任何不需要的堆栈面板... 查看详情

如何根据 Django admin 中的另一个选择标签选项更改选择标签选项?

】如何根据Djangoadmin中的另一个选择标签选项更改选择标签选项?【英文标题】:Howtochangeselecttagoptionsaccordingtoanotherselecttag\'soptioninDjangoadmin?【发布时间】:2020-11-2205:53:37【问题描述】:我在admin.py中有这个classBrandAdmin(admin.ModelAdm... 查看详情

如何根据 android studio 中的微调器选择更改 imageview 视图?

】如何根据androidstudio中的微调器选择更改imageview视图?【英文标题】:howtochangeimageviewviewbasedonspinnerselectioninandroidstudio?【发布时间】:2020-04-2223:19:16【问题描述】:我正在尝试根据微调器选择在图像视图中显示图像。如果有人... 查看详情

需要根据 Listview 中的选择更改全景项目中的选定索引吗? [复制]

】需要根据Listview中的选择更改全景项目中的选定索引吗?[复制]【英文标题】:NeedtochangeselectedindexinPanoramaItembasedonselectioninListview?[duplicate]【发布时间】:2012-04-2813:14:43【问题描述】:可能重复:Navigatebetweenpanoramaitemswp7我在默... 查看详情

如何更改集合视图单元格中的图像

...集合视图单元格中的图像【英文标题】:Howtochangeimageinacollectionviewcell【发布时间】:2014-08-2307:21:10【问题描述】:我有一个带有imageview的CollectionView单元格。问题是我想在选择单元格时更改单元格的图像。有人可以帮帮我吗?我... 查看详情

如何选择 CollectionView 中的最后一个单元格?

】如何选择CollectionView中的最后一个单元格?【英文标题】:HowtoselectthelastcellinaCollectionView?【发布时间】:2015-07-0522:30:21【问题描述】:我有一个包含不同图像的数组,我正在使用“cellForItemAtIndexPath”方法将这些图像添加到Colle... 查看详情

如何根据用户选择的下拉菜单更改 PHP 中的 SQL 查询

】如何根据用户选择的下拉菜单更改PHP中的SQL查询【英文标题】:HowtochangeSQLqueryinPHPinrelationtowhatdropdownmenustheuserselects【发布时间】:2017-05-1013:02:27【问题描述】:我目前正在学校做一个项目,其中涉及使用表单来查询数据库。... 查看详情

如何以编程方式选择collectionview中的项目?

】如何以编程方式选择collectionview中的项目?【英文标题】:Howtoselectitemincollectionviewprogrammatically?【发布时间】:2014-02-0723:05:06【问题描述】:我有一个UICollectionView。UICollectionView的datasource是学生的NSArray(来自CoreData)。我希望... 查看详情

滚动时collectionview更改单元格背景图像

】滚动时collectionview更改单元格背景图像【英文标题】:collectionviewchangecellbackgroundimagewhenscrolling【发布时间】:2016-02-1907:44:43【问题描述】:嗨,我在我的应用程序中使用collectionview。并且当我选择任何单元格时它运行良好,我... 查看详情