繁体   English   中英

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

[英]How to display different cells below CollectionView header depending on users selection?

编辑:我正在尝试重新创建instagram / twitter个人资料页面。 因此,如果我尝试这样做的当前方式不起作用,请随时告诉我如何以完全不同的方式重新创建这些配置文件页面。

我已经设置了一个带标题的UICollectionView。 在这个标题内,有一个menuBar有两个不同的选择选项。 如果用户选择第一个索引,则menuBar返回0.如果用户选择第二个索引,则menuBar返回1。

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CoverCell

            return cell
        }

这是菜单栏和它下面的collectionview单元格的图片: 在此输入图像描述

我也有这个代码来设置单元格:

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let menuBarIndex = profileMenuBarSelectionIndex.menuBarIndexSelection
            var cell: UICollectionViewCell
            switch menuBarIndex {
            case 0:
               cell = collectionView.dequeueReusableCell(withReuseIdentifier: test1CellId, for: indexPath) as! test1Cell
            case 1:
               cell = collectionView.dequeueReusableCell(withReuseIdentifier: test2CellId, for: indexPath) as! test2Cell
            default:
               cell = collectionView.dequeueReusableCell(withReuseIdentifier: test3CellId, for: indexPath) as! test3Cell
            }
            return cell
        }

编辑2:我现在添加了这段代码,其中myTestAction是另一个文件中testAction的委托函数。

var index: Int = 0
func testAction() {
    index = index == 1 ? 0 : 1
    collectionView.reloadData()
}

func myTestAction() {
    delegate?.testAction()
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    switch indexPath.row{
    case 0:
        menuBarIndexSelection = 0
        menuBarIndexChangeFlag = 1
    case 1:
        menuBarIndexSelection = 1
        menuBarIndexChangeFlag = 2
    case 2:
        menuBarIndexSelection = 2
        menuBarIndexChangeFlag = 3
    default:
        menuBarIndexSelection = 0
    }
    myTestAction()
}

我希望能够在标题下方显示不同的单元格,具体取决于menuBar返回的值。 我该怎么做? 或者如何重新创建Instagram个人资料页面?

检查cellForRow和suppviewsViewsForHeader以获取menuBar值。 当menuBar的值改变时,只需重新加载表。 在重新加载时,将根据dataSources中的检查显示数据。

// MENU BAR按钮点击

self.collectionView.reloadData()

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
     if menuBar == 0 {
     // show this image
     }else{
     //show that image
     }
        return cell
    }

尝试这个,

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

//Take a variable menuBar. when the user tap change it's value to 0 or 1  and reload collection view when user tap on menu button.

    if menuBar == 0 {
      return 0
    }else{
      return 1
    }

获取您描述的编译器错误的原因是您创建了4个名为“cell”的不同变量:

var cell: UICollectionViewCell
switch menuBarIndex {
case 0:
    var cell = ...
case 1:
    var cell = ...
default:
    var cell = ...
}
return cell

您在函数末尾返回的单元格实例从未分配过值。 在每种情况下分配第一个单元格变量而不是创建新的单元格应该修复编译器错误:

var cell: UICollectionViewCell
switch menuBarIndex {
case 0:
    cell = collectionView.dequeueReusableCell(withReuseIdentifier: test1CellId, for: indexPath) as! test1Cell
case 1:
    cell = collectionView.dequeueReusableCell(withReuseIdentifier: test2CellId, for: indexPath) as! test2Cell
default:
    cell = collectionView.dequeueReusableCell(withReuseIdentifier: test3CellId, for: indexPath) as! test3Cell
}
return cell

-------------------------编辑---------------------

上面的答案修复了编译器错误,但没有解决实际问题。 我创建了一个小样本项目,可以完成所需的工作并且工作正常。 也许这有助于找出问题所在。 我刚刚在Xcode中创建了一个视图应用程序,这是项目中单个视图控制器和单元格的代码:

class ViewController: UIViewController, UICollectionViewDataSource {

    var index: Int = 0
    var collectionView: UICollectionView?

    override func viewDidLoad() {
        super.viewDidLoad()

        let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 200, height: 50))
        btn.backgroundColor = .green
        btn.setTitle("Change cell color", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.addTarget(self, action: #selector(testAction), for: .touchUpInside)
        view.addSubview(btn)

        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .vertical
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 60, width: view.frame.width, height: view.frame.height - 60), collectionViewLayout: layout)
        collectionView?.backgroundColor = .white
        collectionView?.dataSource = self
        collectionView?.register(RedCell.self, forCellWithReuseIdentifier: "red")
        collectionView?.register(BlueCell.self, forCellWithReuseIdentifier: "blue")
        view.addSubview(collectionView!)
    }

    func testAction() {
        index = index == 1 ? 0 : 1
        collectionView?.reloadData()
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell: UICollectionViewCell
        switch index {
        case 0:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "red", for: indexPath)
        case 1:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "blue", for: indexPath)
        default:
            cell = UICollectionViewCell()
        }
        return cell
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 7
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
        return CGSize(width: 50, height: 50)
    }
}

class RedCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .red
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class BlueCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .blue
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM