簡體   English   中英

通過TableView將XML數組數據傳遞到CollectionView

[英]Passing XML Array Data to CollectionView via TableView

我試圖將數據數組從視圖控制器傳遞到集合視圖單元格。 我的collectionview當前在一個tableview中。 我嘗試使用委托/協議並在類中創建數組,但無法成功將數據傳遞給我的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")
    }

表格視圖單元格:

 class TableViewCell: UITableViewCell {
    var ageUnder10 = [MissingPerson]()
    var age10Plus = [MissingPerson]()
    var age15Plus = [MissingPerson]()
}
  • 這些值是從XML網址填充的
  • 通過掃描儀創建類別,掃描xml中項目的值(以創建ageRange)
  • 我有從視圖控制器類中的單獨數組填充的titleforheader和header名稱

我想通了,我需要使用一個結構來傳遞數據。 另外,在tableview類中創建數組的實例,並編寫一個函數來填充collectionView單元。

例:

CustomTableViewCell:

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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM