gpt4 book ai didi

swift - UICollection View 与 TableView 错误索引超出范围为什么

转载 作者:行者123 更新时间:2023-11-30 10:38:53 27 4
gpt4 key购买 nike

我有字典数组[String:[String]],我想在 TableView 中显示 Collection View ,但是当加载数据时,它会给出错误,指出索引在collectionView的cellForItem中超出范围。这是我的 UIViewController 的完整源代码

    import UIKit
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
} class TableViewController: UITableViewController {
var TestArray = ["Hair": ["1","2","3"],"Nail": ["1","2"],"Makeup":["1","2","3","4"]]
var Sections:[String] = []
var objectArray = [Objects]()
var section = 0
var nameTest:String = ""
override func viewDidLoad() {
super.viewDidLoad()
for (name, path) in TestArray {
// print("The path to '\(name)' is '\(path)'.")
var nameTest = name as? String
// print("value count \(TestArray[name]?.count)")
Sections.append(nameTest!)
}
for (key, value) in TestArray {
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
print("Table View Number of Secltions \(objectArray.count)")
return objectArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as? TableViewCell
// Configure the cell...
section = indexPath.section //cell!.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row] cell?.TestCollectionView.delegate = self cell?.TestCollectionView.dataSource = self cell?.TestCollectionView.reloadData()
return cell!
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 160
}
} extension TableViewController:UICollectionViewDelegate,UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { print("Collection View Number Of Item in Section (objectArray[section].sectionObjects.count)") return objectArray[section].sectionObjects.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as? CollectionViewCell
cell?.Name.text = objectArray[section].sectionObjects[indexPath.row] // here index out of range
return cell!
}
}

最佳答案

您使用了错误的方法,全局初始化部分,并在 cellForRow 中分配其值,因为 cellForRow 被多次调用,其值根据当前显示的单元格而变化。我在代码中做了一些更改,请检查。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as? TableViewCell

// Configure the cell...

section = indexPath.section // remove this line
cell?.TestCollectionView.tag = indexPath.section // add this line
//cell!.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
cell?.TestCollectionView.delegate = self
cell?.TestCollectionView.dataSource = self
cell?.TestCollectionView.reloadData()

return cell!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as? CollectionViewCell
cell?.Name.text = objectArray[section].sectionObjects[indexPath.row] // remove this line
cell?.Name.text = objectArray[collectionView.tag].sectionObjects[indexPath.row]// add this line
return cell!
}

更新

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objectArray[collectionView.tag].sectionObjects.count
}

关于swift - UICollection View 与 TableView 错误索引超出范围为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com