简体   繁体   English

通过TableView将XML数组数据传递到CollectionView

[英]Passing XML Array Data to CollectionView via TableView

I'm attempting to pass an array of data from the view controller to the collection view cells. 我试图将数据数组从视图控制器传递到集合视图单元格。 My collectionview is currently in a tableview. 我的collectionview当前在一个tableview中。 I have tried using delegation/protocols and creating arrays in the class and have not been able to successfully pass the data to my collectionview. 我尝试使用委托/协议并在类中创建数组,但无法成功将数据传递给我的collectionview。

My code is a follows: 我的代码如下:

View Controller: 视图控制器:

var ageUnder10: [MissingPerson] = []
var age10Plus: [MissingPerson] = []
var age15Plus: [MissingPerson] = []

if let ageRange = ageRange {
        switch ageRange {
        case .ageUnder10:
            let ageUnder10Array = MissingPerson()
            ageUnder10Array.title = self.missingPerson.title
            ageUnder10Array.desc = self.missingPerson.desc
            ageUnder10Array.url = self.missingPerson.url
            self.ageUnder10.append(ageUnder10Array)
        case .age10Plus:
            let age10PlusArray = MissingPerson()
            age10PlusArray.title = self.missingPerson.title
            age10PlusArray.desc = self.missingPerson.desc
            age10PlusArray.url = self.missingPerson.url
            self.age10Plus.append(age10PlusArray)
        case .age15Plus:
            let age15PlusArray = MissingPerson()
            age15PlusArray.title = self.missingPerson.title
            age15PlusArray.desc = self.missingPerson.desc
            age15PlusArray.url = self.missingPerson.url
            self.age15Plus.append(age15PlusArray)
        }
    } else {
        print("No valid age found")
    }

Tableview Cell: 表格视图单元格:

 class TableViewCell: UITableViewCell {
    var ageUnder10 = [MissingPerson]()
    var age10Plus = [MissingPerson]()
    var age15Plus = [MissingPerson]()
}
  • These values are being populated from an XML url 这些值是从XML网址填充的
  • The categories are being created via scanner, scanning the values of a item in the xml (to create ageRange) 通过扫描仪创建类别,扫描xml中项目的值(以创建ageRange)
  • I have titleforheader and header names populated from a separate array in the view controller class 我有从视图控制器类中的单独数组填充的titleforheader和header名称

I figured it out, I needed to use a struct to pass the data. 我想通了,我需要使用一个结构来传递数据。 Also, create an instance of the array in the tableview class and write a function to fill the collectionView cell. 另外,在tableview类中创建数组的实例,并编写一个函数来填充collectionView单元。

Example: 例:

CustomTableViewCell: CustomTableViewCell:

customArray: [CustomArray]()

func configureCollectionCell(with array: [CustomArray]) {
self.customArray = customArray
}

ViewController Class: ViewController类别:

 var customArray = [CustomArray]()

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   if let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomTableViewCell {
    cell.configureCollectionCell(with: customArray)
    return cell
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM