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

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

我正在尝试将视图控制器中的数据数组传递给集合视图单元格。我的收藏视图目前处于桌面视图中。我尝试过使用委托/协议并在类中创建数组,但是无法将数据成功传递给我的collectionview。

我的代码如下:

查看控制器:

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单元格:

 class TableViewCell: UITableViewCell {
    var ageUnder10 = [MissingPerson]()
    var age10Plus = [MissingPerson]()
    var age15Plus = [MissingPerson]()
}
  • 这些值是从XML URL填充的
  • 通过扫描程序创建类别,扫描xml中项目的值(以创建ageRange)
  • 我有一个titleforheader和标题名称从视图控制器类中的一个单独的数组填充
ios swift xml uitableview uicollectionview
1个回答
0
投票

我想通了,我需要使用结构来传递数据。此外,在tableview类中创建数组的实例,并编写一个函数来填充collectionView单元格。

例:

自定义TableViewCell:

customArray: [CustomArray]()

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

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
    }
© www.soinside.com 2019 - 2024. All rights reserved.