[英]How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell?
-大家好,我有UIViewContoller,包括TableView和collectionView。 tableView具有三個部分,第三個包含動態單元格。第三個包含collectionView的單元格也是動態的。
-問題出在CollectionView上
我有3個數組,每個數組都包含圖像名稱
var dataSheet = ["1", "2", "3"]
var partList = ["4", "5", "6"]
var drawing = ["7", "8", "9"]
我應該將這些數組顯示在一個collectionViewCell中,我不想將它們添加到一個數組中,因為當我運行應用程序並打開此頁面並且嘗試選擇collectionView的最后一個索引時,每個數組都屬於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)
}
我為在一個collectionView中添加3個數組所做的工作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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.