如何使用 swift 在 UITableview 的集合视图中填充不同的数组值?

How to populate different array values in collection view inside of UITableview using swift?

我在 UITableView 中创建了 UICollectionView,在 UItableview 中填充数组值工作完美,当尝试将数组值填充到 collectionView 中时,在 collectionView 中获得相同的值,我想填充第零个索引第零行 UItableview 的 collectionview 中的数组等等 下面我试了一个例子,

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource{


        let array = ["product 1","product 2","product 3","product4"]
        let array1 = ["product id 1","product id 2","product id 3","product id 4"]
        let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]] as [NSArray]


     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
            cell.tableviewlabel1.text = array[indexPath.row]
             cell.tableviewlabel2.text = array1[indexPath.row]
            return cell
        }
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return array2.count
        }

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


            let rowValue : NSArray = array2[indexPath.row]
            for i in 0..<rowValue.count {
            cell.collectionviewlabel1.text = rowValue[i] as? String
            }
            return cell

        }

例如,我想要在第零行表视图的集合视图中的 array2 值的第零个索引,以及在表视图第一行的集合视图中的第一个索引值等等。

举个例子:

第一个 tableView 在里面 ViewController

 class ViewController: UIViewController {

//MARK:- IBOUTELTS
//MARK:-
@IBOutlet weak var tableView: UITableView!

//MARK:- VARIABLES
//MARK:-

override func viewDidLoad() {
    super.viewDidLoad()


}


}

extension YourViewConroller: UITableViewDataSource, UITableViewDelegate {

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


}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {


}

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


}

}

并且您的 CollectionView 在 TableViewCell 中

 extension YourTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

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


}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

}

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



}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}


}

并为 TableView 和 CollectionView 设置不同的class:

 class YourTableViewCell: UITableViewCell { // tableview cell

}


 classYourCollectionViewCell: UICollectionViewCell { // collectionviewcell

  }

首先你的array2必须是这样的:

let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]]

不要在 Swift 中使用 NSArray。你可以把它写成 [String], [[String]].

您的 ViewController 将只有 UITableViewDelegateUITableViewDatasource 方法:

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
        cell.tableviewlabel1.text = array[indexPath.row]
        cell.tableviewlabel2.text = array1[indexPath.row]

        cell.arrayForCollectionView = array2
        return cell
    }
}

然后在你的 TableViewCell:

class TableViewCell: UITableViewCell {

    var arrayForCollectionView : [[String]]! {
        didSet {
            self.collectionView.reloadData
        }
    }

    @IBOutlet weak var collectionView: UICollectionView!

}

extension TableViewCell : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (arrayForCollectionView != nil) {
            return arrayForCollectionView.count
        }
        return 0
    }

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


        let rowValue = arrayForCollectionView[indexPath.row]
        for i in 0..<rowValue.count {
            cell.collectionviewlabel1.text = rowValue[i] as? String
        }
        return cell
    }
}

如果您遇到任何问题,请告诉我。