如何设置段控件以更改 table 视图单元格中的集合单元格项?

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

我在 table 视图单元格中创建了一个集合视图,并在 table 视图的导航栏上添加了一个段控件。当段出口和操作在 table 视图控制器上时,如何更改收集单元格项目?

我在 table 视图控制器上试过这个但是得到这个错误: 当集合视图中只有 1 个部分时,请求第 2147483647 部分之前的项目数

@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 查看单元控制器:

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
}

我想在 Segment 更改后将 cateLabel.text! = nameArray[indexPath.row] 更改为 nameArray2[indexPath.row],如何操作?

在 Action 上调用 tableView.reloadData() 并在 cellForRowAt

上调用 collectionView.reloadData()
@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


}

在 table 视图单元格上 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
}