如何根据segmentedControl索引更改数据

时间:2019-11-25 07:05:48

标签: ios swift uicollectionview uisegmentedcontrol

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

  

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

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

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

 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


    }

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "homeViewReuseCell", for: indexPath) as! HomeViewHeaderReusableView

        headerView.delegate = self 
        return headerView
    }


修改

这是标题中的代码


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



    }


2 个答案:

答案 0 :(得分:1)

永远不会,永远不会dequeueReusableCell之外致电cellForRow/ cellForItem。单元已出队,但在方法退出时被释放。与收集/表格视图数据源方法不同,该单元格不会在任何地方返回。

编辑:

替换

var header: HomeViewHeaderReusableView?

使用

var selectedSegmentIndex = 0

然后将整个方法handleSegControlTapped替换为

func handleSegControlTapped(for header: HomeViewHeaderReusableView) {
    selectedSegmentIndex = header.segmentedControl!.selectedSegmentIndex
    collectionView.reloadData()
}

当数据源方法处理分段控件的状态时,只需重新加载集合视图即可。

然后将numberOfItemsInSection更改为

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

    switch selectedSegmentIndex {
    case 0: return femaleTrends.count
    case 1: return maleTrends.count
    default: print("opps, cant load data")
       return 0
    }
}

,并相应地更改其他数据源方法。

答案 1 :(得分:0)

根据您对细分的选择,仅重新加载collectionView并在CollectionViewCell内配置cellForItemAt

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

相关问题