我的情况很复杂,我需要你的帮助来弄清楚我该怎么做。
我有一个原型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])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句