ios - Swift中使用自定义分段控件时CollectionCell数据显示问题

标签 ios swift uicollectionview uisegmentedcontrol

我引用“http://bitly.kr/fDz2Ma”做了一个自定义分段控件。但是,当我选择一个特定的项目时,我需要查看与该项目匹配的 Collection View 单元格,但我没有找到分段控件的选定索引。

到目前为止,我已经创建了自定义分段控件,但还没有找到选定的索引按钮项。

import UIKit

class MainBookViewController: UIViewController {

@IBOutlet weak var interfaceSegmented: CustomSegmentedControl! {
    didSet{
        interfaceSegmented.setButtonTitles(buttonTitles: ["A","B","C"])
        interfaceSegmented.selectorViewColor = .red
        interfaceSegmented.selectorTextColor = .red
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    codeSegmentedConfig()
}

func codeSegmentedConfig() {
    let codeSegmented = CustomSegmentedControl(frame: CGRect(x: 0, y: 90, width: self.view.frame.width, height: 50), buttonTitle: ["A","B","C""])
    codeSegmented.backgroundColor = .clear
    view.addSubview(codeSegmented)
}

}

extension MainBookViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

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

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

        cell.bookTitleLabel.text = "apple"
        cell.bookWriterLabel.text = "me"
        cell.bookImageView.image = UIImage(named: "3")

        return cell
    }
}

如果我只知道选中的自定义分段控件的按钮索引,我想使用case语句将 Collection View 单元格中的数据更改为索引号。

如果我只知道所选按钮项的索引,我打算使用 CollectionViewDataSource 代码中的 case 语句修改 Collection View 单元格。

最佳答案

如果我正确理解了您的问题,您可以通过执行以下操作来实现:

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

        switch interfaceSegmented.selectedSegmentIndex {
          case 0:
            //do something when selected segment index is 0
          case 1:
            //do something when selected segment index is 1
          default:
            break
    }

关于ios - Swift中使用自定义分段控件时CollectionCell数据显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56997392/

相关文章:

ios - UICollectionView 在滚动时重新加载错误的图像

android - 在代号中,一个组合框文本被截断

ios - 如何使 UIImage 不是文件所有者的对象?

ios - 如何通过单击 WKWebView 中的按钮打开社交链接

ios - 使用 UISplitViewController 的 Tableview 作为过滤器

ios - Google Plus 共享显示无效地址错误 ios

ios - Facebook SDK 启动到 Safari 而不是应用程序 (Swift)

ios - 将两个变量传递给同一个 View Controller swift

ios - Swift - 使用 UIImage 动画呈现/关闭 ViewController

ios - UICollectionView 在重新加载数据时滚动不流畅