如何基于segmentedControl索引更改数据

问题描述 投票:0回答:1

我正在尝试根据段控件的索引更改collectionView的数据。当我运行代码时,我崩溃了。有人可以告诉我我做错了什么吗?我遵循了发现的不同方法。我在崩溃中收到的错误代码是

线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x1da72fde0)

当我尝试在段索引之间切换时发生错误。并发生在线

cell.trendsLabel.text = maleTrends[indexPath.row]

of

 func handleSegControlTapped(for header: HomeViewHeaderReusableView)


 var header: HomeViewHeaderReusableView?



    func handleSegControlTapped(for header: HomeViewHeaderReusableView) {

        collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems) 

        switch header.segmentedControl?.selectedSegmentIndex {
        case 0:
            print("Display female trends")

            header.segmentedControl?.selectedSegmentIndex = 0

            let indexPath = IndexPath()
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homeViewCell", for: indexPath) as! CurrentTrendsCell
            cell.trendsLabel.text = femaleTrends[indexPath.row]
            cell.trendsImageView.image = femaleImages[indexPath.row]

        case 1:
            print("Display male trends")
            header.segmentedControl?.selectedSegmentIndex = 1
            let indexPath = IndexPath()

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homeViewCell", for: indexPath) as! CurrentTrendsCell
                     cell.trendsLabel.text = maleTrends[indexPath.row]
            cell.trendsImageView.image = maleImages[indexPath.row]

        default:
            break
        }

        collectionView.reloadData()

    }



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

        let segmentedOutlet = header?.segmentedControl

        switch segmentedOutlet?.selectedSegmentIndex {
        case 0: return femaleTrends.count

       case 1: return maleTrends.count

        default: print("opps, cant load data")
        }

        return 0

    }


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

        let segmentedOutlet = header?.segmentedControl

        switch segmentedOutlet?.selectedSegmentIndex {
        case 0: cell.trendsLabel.text = femaleTrends[indexPath.row]
        cell.trendsImageView.image = femaleImages[indexPath.row]

        case 1: cell.trendsLabel.text = maleTrends[indexPath.row]
        cell.trendsImageView.image = maleImages[indexPath.row]

        default: break

        }
        return cell


    }

编辑

这是标题中的代码


class HomeViewHeaderReusableView: UICollectionReusableView {

    @IBOutlet weak var segmentedControl: UISegmentedControl?


    var delegate: HomeSegmentedControl?



     // MARK: Handler
    @objc func handleSegTapped() {
        delegate?.handleSegControlTapped(for: self)
    }



    @IBAction func segmentedTapped(_ sender: Any) {
        handleSegTapped()

        // change data based on female / male


    }


 override func awakeFromNib() {
         super.awakeFromNib()

        segmentedControl?.tintColor = .clear
        segmentedControl?.layer.borderColor = UIColor.clear.cgColor
        segmentedControl?.setBackgroundImage(UIImage(), for: .normal, barMetrics: .default)


        segmentedControl?.setTitleTextAttributes([
            NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 11),
            NSAttributedString.Key.foregroundColor: UIColor.lightGray

            ], for: .normal)

        segmentedControl?.setTitleTextAttributes([
            NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 11) ,
            NSAttributedString.Key.foregroundColor: UIColor.white
            ], for: .selected)


        segmentedControl?.selectedSegmentIndex = 0



    }


ios swift uicollectionview uisegmentedcontrol
1个回答
0
投票

根据您在段上的选择,仅重新加载collectionView并在'cellForItemAt'中配置'CollectionViewCell'。

无论您在何处添加了细分控件,都只能对操作方法做出真正的反应并相应地更新集合。

© www.soinside.com 2019 - 2024. All rights reserved.