ios - 如何使用 swift 在 UITableview 的 Collection View 中填充不同的数组值?

标签 ios swift uitableview swift3 uicollectionview

我在 UITableView 内部创建了 UICollectionView,在 UItableview 中填充数组值工作完美,当尝试将数组值填充到 collectionView 中时,在 collectionView 中获得相同的值,我想在 tableView 的所有行中填充第零个索引数组第零行 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 值在第 0 行的 collectionview 中的第 0 个索引,以及在 tableview 的第一行的 collectionview 中的第一个索引值等等。

最佳答案

首先你的 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
    }
}

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

关于ios - 如何使用 swift 在 UITableview 的 Collection View 中填充不同的数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757453/

相关文章:

Swift:比较泛型类中的泛型类型

ios - 如何使用 UIDocumentPicker 将 pdf 文件导入 TableView 单元格

swift - 如何在 Swift 中创建泛型协议(protocol)?

ios - 我在表格 View 中选择了一系列地点类别,但在 segue 中它只发送一个

objective-c - 自定义 UITableViewCell 不占据单元格的完整边界?

ios - 重新加载源数据iOS后,使用动态高度单元保持UITableView单元位置

ios - 带有部分和滑动操作的 SwiftUI 列表一次加载所有行

ios - React-native run-ios加载环境变量

ios - 使 CollectionView 和 TableView 高度动态化

ios - 在 SwiftUI 中填写父项