首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使CollectionViewCell已按下

使CollectionViewCell已按下
EN

Stack Overflow用户
提问于 2020-06-14 12:48:12
回答 1查看 27关注 0票数 1

我有一个带有服务的控制器,我希望我的第一个服务看起来应该已经按下了。不知道该怎么做。我尝试了方法WillDisplay和什么都没有。在cellForItemAt中也尝试过第一个单元格isSelected = true。一样的。不管用。

在这里输入图像描述

代码语言:javascript
复制
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as? ServiceCollectionViewCell
    let models = self.serviceModel[indexPath.row]

    switch models {
    case .haircut:
        self.pricingModel?.haircut = true
        cell?.setSelected()
        print(pricingModel?.haircut as Any)
    case .babyHaircut:
        self.pricingModel?.babyHaircut = true
        cell?.setSelected()
        print(pricingModel?.babyHaircut as Any)
    case .beardHaircut:
        self.pricingModel?.beardHaircut = true
        cell?.setSelected()
        print(pricingModel?.beardHaircut as Any)
    case .straightRazor:
        self.pricingModel?.straightRazor = true
        cell?.setSelected()
        print(pricingModel?.straightRazor as Any)
    case .clipper:
        self.pricingModel?.clipper = true
        cell?.setSelected()
        print(pricingModel?.clipper as Any)
    case .stacking:
        self.pricingModel?.stacking = true
        cell?.setSelected()
        print(pricingModel?.stacking as Any)
    }
}


func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? ServiceCollectionViewCell
        let models = self.serviceModel[indexPath.row]

        switch models {
        case .haircut:
            self.pricingModel?.haircut = false
            cell?.setUnselected()
            print(pricingModel?.haircut as Any)
        case .babyHaircut:
            self.pricingModel?.babyHaircut = false
            cell?.setUnselected()
            print(pricingModel?.babyHaircut as Any)
        case .beardHaircut:
            self.pricingModel?.beardHaircut = false
            cell?.setUnselected()
             print(pricingModel?.beardHaircut as Any)
        case .straightRazor:
            self.pricingModel?.straightRazor = false
            cell?.setUnselected()
            print(pricingModel?.straightRazor as Any)
        case .clipper:
            self.pricingModel?.clipper = false
            cell?.setUnselected()
            print(pricingModel?.clipper as Any)
        case .stacking:
            self.pricingModel?.stacking = false
            cell?.setUnselected()
            print(pricingModel?.stacking as Any)
    }
}

这里是我的代码

EN

回答 1

Stack Overflow用户

发布于 2020-06-16 12:15:01

下面是一个示例,当视图第一次加载时,我希望它显示蓝色,即项目1。

代码语言:javascript
复制
let dataSource = ["Red", "Blue", "Purple", "Brown"]

override func viewDidLoad() { // or viewWillAppear depending on your needs
    super.viewDidLoad()

    autoShowPreselectedCell()
}

func autoShowPreselectedCell() {

    let blueItem = dataSource[1]

    // 1. Create an IndexPath using the item and section. For this example there is only 1 section
    let preSelectedIndexPath = IndexPath(item: blueItem, section: 0)

    // 2. this is where it preselects
    yourCollectionView.selectItem(at: preSelectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)
    collectionView(self.yourCollectionView, didSelectItemAt: preSelectedIndexPath)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62372746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档