Search code examples
swiftuitableviewuicollectionviewcelluisegmentedcontrol

How to set a segment control to change collection cell item in table view cell?


I created a collection view in table view cell and added a segment control on the navigation bar of the table view. How can I change the collection cell item when the segment outlet and action is on table view controller?

I tried this on table view controller but get this error: request for number of items before section 2147483647 when there are only 1 sections in the collection view

@IBAction func mySegAction(_ sender: UISegmentedControl) {
    switch mySeg.selectedSegmentIndex
    {
    case 0:
        print("0")
    case 1:
        print("1")
        let indexPath = IndexPath()
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CategoryCollectionViewCell

        cell.cateLabel.text! = nameArray2[indexPath.row]
    default:
        break;
    }
}

Table view cell controller:

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

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

    cell.cateLabel.text! = nameArray[indexPath.row]
    // I tried this code but fail
    if AddViewController().mySeg?.selectedSegmentIndex == 1 {
        cell.cateLabel.text! = nameArray2[indexPath.row]
    }
    return cell
}

I want to change cateLabel.text! = nameArray[indexPath.row] to nameArray2[indexPath.row] when the Segment is changed, How to do that?


Solution

  • Calling tableView.reloadData() on Action and collectionView.reloadData() on cellForRowAt

    @IBAction func mySegAction(_ sender: UISegmentedControl) {
        switch mySeg.selectedSegmentIndex{
            case 0:
                segment = 0
                UserDefaults.standard.setValue(segment, forKey:"segment")
                UserDefaults.standard.synchronize()
            case 1:
                segment = 1
                UserDefaults.standard.setValue(segment, forKey:"segment")
                UserDefaults.standard.synchronize()
            default:
                break;
        }
        self.tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        if indexPath.row == 1{
            let cell1 = tableView.dequeueReusableCell(withIdentifier: "AddViewCell2", for: indexPath)
            return cell1
        }else if indexPath.row == 2{
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "AddViewCell3", for: indexPath)
            return cell2
        }else if indexPath.row == 3{
            let cell3 = tableView.dequeueReusableCell(withIdentifier: "AddViewCell4", for: indexPath)
            return cell3
        }else if indexPath.row == 4{
            switch segment {
            case 0:
                let cell4 = tableView.dequeueReusableCell(withIdentifier: "AddViewCell5", for: indexPath) as! AddViewCell
                cell4.collectionView.reloadData()
                return cell4
            case 1:
                let cell4 = tableView.dequeueReusableCell(withIdentifier: "AddViewCell5", for: indexPath) as! AddViewCell
                cell4.collectionView.reloadData()
                return cell4
            default:
                break
            }
        }
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "AddViewCell1", for: indexPath)
        return cell
    
    
    }
    

    On the table view cell class:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CategoryCollectionViewCell
        let segment = UserDefaults().value(forKey: "segment") as! Int
        //print("segment here")
        //print(segment)
        switch segment {
        case 0:
            cell.cateLabel.text! = nameArray[indexPath.row]
            cell.cateImg.image = imageName[indexPath.row]
        case 1:
            cell.cateLabel.text! = nameArray2[indexPath.row]
            cell.cateImg.image = imageName[indexPath.row]
        default:
            break;
        }
        return cell
    }