如何使用包含集合视图的表视图设置委托?

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

因此,我有一个包含UISegmentedControl的视图控制器,一个带有2个单元格的表视图,在这两个表视图单元格中,我每个单元格都有一个集合视图,这些视图应根据所选的段索引显示不同的数据

点击分段控件时,我想在每个集合视图中显示不同的数据。当我点击细分控件时,什么也没有发生,索引保持在0。我在多行上运行了一个断点,我的代表回到了零。

我如何正确设置代表?我以为表格视图单元格的self.shareVC?.styleDelegate = self中的self.shareVC?.trendDelegate = selfawakeFromNib()会设置它,但它仍然返回nil。

此外,如果我手动将selectedSegmentIndex设置为1,则会显示男性数据是否应有,反之亦然。

主VC

class SharePhotoViewController: UIViewController, UITextViewDelegate, UITableViewDelegate, UITableViewDataSource  {


    @IBOutlet weak var segmentedControl: UISegmentedControl?


      var selectedSegmentIndex = 0
    // 
      var trendDelegate: TrendsTabelCellDelegate?
      var styleDelegate: StylesTableCellDelegate?


   // MARK: - TableView 

 func numberOfSections(in tableView: UITableView) -> Int {
          return 1
       }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        2
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        switch indexPath.row {
            case 0:
                let trendingCell = tableView.dequeueReusableCell(withIdentifier: "currentTrends", for: indexPath) as! TrendsTableCell

                return trendingCell
            case 1:
                let styleCell = tableView.dequeueReusableCell(withIdentifier: "styles", for: indexPath) as! StyleTableCell

                return styleCell
            default:
                return UITableViewCell()
            }

    }

//display data based on segmentIndex
    @IBAction func segmentTapped(_ sender: Any) {
        print(selectedSegmentIndex)
        trendFunc()
        styleFunc()

    }


    @objc func trendFunc() {
        trendDelegate?.handleSegControlTapped(for: self)


      }

    @objc func styleFunc() {
        styleDelegate?.handleSegmentControlTapped(for: self)

        }


}

TableView单元格(包含集合视图)

class TrendsTableCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, TrendsTabelCellDelegate {


    var selectedSegmentIndex = 0
    var shareVC: SharePhotoViewController?



    // MARK: - Delegate Func
    func handleSegControlTapped(for view: SharePhotoViewController) {

        selectedSegmentIndex = view.segmentedControl!.selectedSegmentIndex
       collectionView.reloadData()
    }


    // MARK: - Collection View
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1 
    }


    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
        }
    }

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

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

        switch selectedSegmentIndex {
               case 0: cell.trendLabel.text = femaleTrends[indexPath.row].name
               cell.trendIV.image = femaleTrends[indexPath.row].image


               case 1: cell.trendLabel.text = maleTrends[indexPath.row].name
               cell.trendIV.image = maleTrends[indexPath.row].image

               default: break

               }

            cell.layer.cornerRadius = 9.0
            cell.layer.borderWidth = 0.1
            cell.layer.borderColor = UIColor.lightGray.cgColor


        return cell



    }
class StyleTableCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, StylesTableCellDelegate {


    var shareVC: SharePhotoViewController?
    var selectedSegmentIndex = 0


    func handleSegmentControlTapped(for view: SharePhotoViewController) {
      selectedSegmentIndex = view.segmentedControl!.selectedSegmentIndex
      collectionView.reloadData()
    }


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

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

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



       switch selectedSegmentIndex {
       case 0: cell.styleLabel.text = femaleStyles[indexPath.row].name
       cell.styleIV.image = femaleStyles[indexPath.row].image



       case 1: cell.styleLabel.text = maleStyles[indexPath.row].name
       cell.styleIV.image = maleStyles[indexPath.row].image

       default: break

       }


        cell.layer.cornerRadius = 9.0
        cell.layer.borderWidth = 0.1
        cell.layer.borderColor = UIColor.lightGray.cgColor

        return cell
    }

    ```
ios swift uitableview uicollectionview uisegmentedcontrol
1个回答
0
投票
检查以下示例。有关更多详细信息,请继续我的example project

TableViewCell

class DashboardTableViewCell: UITableViewCell { func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) { collectionView.delegate = dataSourceDelegate collectionView.dataSource = dataSourceDelegate collectionView.tag = row collectionView.setContentOffset(collectionView.contentOffset, animated: false) collectionView.reloadData() } var collectionViewContentOffsett: CGFloat { set { collectionView.contentOffset.x = newValue } get { return collectionView.contentOffset.x } } }

UIViewController

final class JobsListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as DashboardTableViewCell cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) cell.collectionViewContentOffsett = storedOffsets[indexPath.row] ?? 0 cell.selectionStyle = .none return cell } } extension JobsListViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 75, height: 75) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return jobList[collectionView.tag].connectedBusinesses?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell: BusinessCollectionViewCell = collectionView.dequeueReusableCell(for: indexPath) let data = jobList[collectionView.tag].connectedBusinesses?[indexPath.item] cell.configure(data) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { DDLogInfo("Collection view at row \(collectionView.tag) selected index path \(indexPath)") } }
© www.soinside.com 2019 - 2024. All rights reserved.