-您好,我有UIViewContoller,包括TableView和collectionView。 tableView具有三个部分,第三个包含动态单元格。第三个包含collectionView的单元格也是动态的。
-问题出在CollectionView上,
我有3个数组,每个数组都包含图片名称
var dataSheet = ["1", "2", "3"]
var partList = ["4", "5", "6"]
var drawing = ["7", "8", "9"]
我应该将这些数组显示在一个collectionViewCell中,我不想将它们添加到一个数组中,因为当我运行应用程序并打开此页面并且尝试选择最后一个索引时,每个数组都属于tableview中的不同行查看我的应用程序崩溃,并告诉我indexPath超出范围。
// TableView代码
func numberOfSections(in tableView: UITableView) -> Int {
return 4
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1 :
return 1
case 1 :
return 1
case 2 :
return tabelviewSectionNumber.count
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let ComponentkksCell = tableView.dequeueReusableCell(withIdentifier: "componentKKS", for: indexPath) as! componentKKS_TVC
ComponentkksCell.setUp(data: DescriptionArray[indexPath.row])
return ComponentkksCell
case 1 :
let SystemDescriptionCell = tableView.dequeueReusableCell(withIdentifier: "SystemDescription_TVC", for: indexPath) as! SystemDescription_TVC
SystemDescriptionCell.setUp(data: DescriptionArray[indexPath.row])
return SystemDescriptionCell
case 2:
let DataSheetCell = tableView.dequeueReusableCell(withIdentifier: "DataSheet_TVC", for: indexPath) as! DataSheet_TVC
DataSheetCell.setTableViewDelegateAndDataSource(dataSourceDelegate: self, forRow: indexPath.row)
DataSheetCell.titlesections.text = tabelviewSectionNumber[indexPath.row]
self.indexPath.append(indexPath.row)
return DataSheetCell
default:
return UITableViewCell()
}
// collectionView代码
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if shareVar.shared.mech == true {
return getCurrentArray()?.count ?? 0
} else if shareVar.shared.elec == true {
return getCurrentArray()?.count ?? 0
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let dataCell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataImages", for: indexPath) as! SEH_DRA_COMCell
if let currentImage = getCurrentArray()?[indexPath.row]{
if shareVar.shared.mech == true {
dataCell.imageData.image = UIImage(named: currentImage )
} else if shareVar.shared.elec == true {
dataCell.imageData.image = UIImage(named: currentImage )
}
}
return dataCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let currentImage = getCurrentArray()?[indexPath.item] else {return}
print(indexPath.item)
print(currentImage)
presenter?.didSelectImage(image: currentImage)
}
我将3个数组添加到一个collectionView单元格中的操作 1-我创建了2个阵列
var elecData:[Int:[String]] = [:] var mechData:[Int:[String]] = [:] 2,然后我给每个数组添加标记并将其添加到我创建的那些数组中
self.mechData [0] = dataSheet self.mechData [1] = partList self.mechData [2] =绘图 self.elecData [0] = dataSheet self.elecData [1] =接线
3-此函数可生成当前数组
func getCurrentArray() -> [String]? {
var array = [String]()
if shareVar.shared.mech == true {
for index in indexPath {
array = self.mechData[index] ?? [""]
}
} else if shareVar.shared.elec == true{
for index in indexPath {
array = self.elecData[index] ?? [""]
}
}
return array
}