首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有集合视图的自定义菜单栏

带有集合视图的自定义菜单栏
EN

Stack Overflow用户
提问于 2020-01-19 15:49:41
回答 1查看 533关注 0票数 0

我想问一下。我从collectionView制作了一个自定义菜单栏,我想在滑动时链接和更改我的收藏视图的菜单栏和来自另一个collectionView的数据。这是一张我正在尝试制作的图片

但是,当我尝试向左和向右滑动时,我的菜单栏没有跟随,我已经进行了一个引用来捕获该值。但它仍然不起作用。以下是我的代码

代码语言:javascript
复制
class SegmentedView: UIView {

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        return cv
    }()

    let knowledge = ["Pengetahuan", "Keterampilan", "Sikap"]
    var selectedMenu: CGFloat?

    }

extension SegmentedView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return knowledge.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SegmentedCell
        let item = knowledge[indexPath.item]
        cell.nameLabel.text = item

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedMenu = CGFloat(indexPath.item) * frame.width
    }
}

// This from my SegmentedViewController
class SegmentedViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var segmentedViews: SegmentedView!

    let cellId = "assesmentCell"
    let colors: [UIColor] = [UIColor.blue, UIColor.yellow, UIColor.green]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupCollection()
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset.x / 3)
        segmentedViews.selectedMenu = scrollView.contentOffset.x / 3
    }

    func setupCollection() {
        if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.scrollDirection = .horizontal
            flowLayout.minimumLineSpacing = 0
        }
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
    }
}

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-01-19 18:54:30

您的selectedMenu属性不侦听具有didSet property observer的事件。因此,当您在scrollViewDidScroll中设置selectedMenu时,什么也不会发生。

这可能是一种解决方案:

代码语言:javascript
复制
var selectedMenu: CGFloat? {
    didSet {
        collectionView.selectItem(at: <#T##IndexPath?#>, animated: <#T##Bool#>, scrollPosition: <#T##UICollectionView.ScrollPosition#>)
    }
}

但是要小心SegmentedView的collectionView(_:didSelectItemAt:)方法,它可能会带来一些不想要的行为。您可以使用委托模式来触发另一个类中的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59808182

复制
相关文章

相似问题

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