我的情况很复杂,我需要你的帮助来弄清楚我应该做什么。
我有一个原型(prototype)UIcollectionView
,这个原型(prototype)应该为每种样式类型创建4次。
我将这些样式类型定义为枚举:
enum Colors {
case black, blue, red, green
}
var color = Colors.black
在CollectionViewCell
内部,我还有一个tableView
,它有一个包含标签的原型(prototype)。 TableViews
应该由四个数组填充:
var black = ["black1","black2","black3"]
var blue = ["blue1","blue2","blue3"]
var red = ["red1","red2","red3"]
var green = ["green1","green2","green3"]
现在,我尝试在这些 TableView 和 collectionView 之间建立连接
首先是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])
关于ios - 在不同的collectionView单元格内填充不同的tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284422/