简体   繁体   中英

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

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
  • The categories are being created via scanner, scanning the values of a item in the xml (to create ageRange)
  • I have titleforheader and header names populated from a separate array in the view controller class

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.

Example:

CustomTableViewCell:

customArray: [CustomArray]()

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

ViewController Class:

 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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