![](/img/trans.png)
[英]How to embed some tableViews with different data in a collectionView?
[英]Fill different tableViews inside the different collectionView cell
我有一個復雜的情況,我需要你的幫助來弄清楚我該怎么做。
我有一個原型UIcollectionView
,這個原型應該為每種樣式類型創建4次。 我將這些樣式類型定義為枚舉:
enum Colors {
case black, blue, red, green
}
var color = Colors.black
在CollectionViewCell
里面我還有一個tableView
,它有一個包含標簽的原型。 並且這些數組應該由TableViews
填充四個數組:
var black = ["black1","black2","black3"]
var blue = ["blue1","blue2","blue3"]
var red = ["red1","red2","red3"]
var green = ["green1","green2","green3"]
現在,我試圖在這些TableViews和collectionViews之間建立連接
首先是UICollectionView
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.row {
case 0,1,2,3:
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colors", for: indexPath) as? colorsCell {
switch indexPath.row {
case 1:
self.color = .black
case 2:
self.color = .blue
case 3:
self.color = .red
case 4:
self.color = .green
default:
break
}
return cell
}
default:
break
}
return UICollectionViewCell()
}
然后,對於TableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0,1,2,3:
if let cell = tableView.dequeueReusableCell(withIdentifier: "colCell", for: indexPath) as? colCellDashboard {
switch self.color {
case .black:
cell.title.text = black[indexPath.row]
case .blue:
cell.title.text = blue[indexPath.row]
case .red:
cell.title.text = red[indexPath.row]
case .green:
cell.title.text = green[indexPath.row]
}
return cell
}
return UITableViewCell()
}
結果不夠好,前三個collectionview中的前三個tableview由藍色數組填充,最后一個是正確的,填充綠色數組。
如果你能幫助我,我將不勝感激。
當tableView嵌套在你應該使用的集合中時
class CollectionCell:UICollectionViewCell,UITableViewDelegate,UITableViewDataSource {
var tableArr = [String]() // table dataSource array
func configure(_ res:[String]) {
tableArr = arr
self.tableView.reloadData()
}
///////
here implement the cellForRowAt and numberOfRows for the nested tableView
}
在包含collectionView的vc內部聲明了這樣的數組
let arr = [["black1","black2","black3"] , ["blue1","blue2","blue3"] ,
["red1","red2","red3"] , ["green1","green2","green3"]]
然后在cellForItemAt
里面
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colors", for: indexPath) as? colorsCell {
cell.configure(arr[indexPath.row])
啊,你的代碼問題是在你的cellForItemAt
集合視圖的委托方法中,你正在為類變量self.color
分配顏色。 每次調用代理時都會覆蓋此內容。 因此,當調用tableview的cellForRowAt
委托時,它將具有self.color
的覆蓋值(無論self.color
是最新的值),因此您將在表中看到意外的條目。
在collectionViews中有tableview是一個常見的問題。 解決它的最簡單方法是:
- 在collectionViewCell中輸入tableview的數據。(在UICollectionViewCell
子類中創建一個變量。)
- 為CollectionView的cellForItemAt
委托中的每個tableView的數據源cellForItemAt
。
- 在collectionViewCell中寫入所有tableViewDelegates,並使用CollectionViewCell的數據源變量來填充tableView。
關鍵是underStand tableView在CollectionViewCell內部,因此,您的CollectionViewCell負責創建tableView。
給出的CollectionViewCell結構是@Sh_Khan看起來很好(所以不要使用類似的代碼)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.