【发布时间】:2017-06-27 16:27:17
【问题描述】:
我有一个 UITableView 有一个 UICollectionView 和
我使用numberOfRowsInSection 设置行数并使用cellForRowAt 显示带有标题的单元格:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return sectionsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let strname = sectionsArray[indexPath.row]
if (strname as AnyObject).isEqual(to: "top"){
tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle
tableCellobj.lblMiddle.text = "\(strname)"
return tableCellobj
}
if (strname as AnyObject).isEqual(to: "bottom") {
tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle
tableCellobj.lblMiddle.text = "\(strname)"
return tableCellobj
}
如您所见,数组sectionsArray 是:
var sectionsArray: NSMutableArray = NSMutableArray()
sectionsArray.add("top")
sectionsArray.add("bottom")
现在表格显示了两行标题“顶部”和“底部”
我有两个不同的数据NSArray 显示在 CollectionView 中:
top = ["1","2","3"]
bottom = ["4","5","6"]
collectionView 有一个标签(在 Storyboard 中标识),所以我使用标签来定义视图和显示数据:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
if collectionView.tag == 2 {
objCollectionViewCellMiddle = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle"
, for: indexPath) as! CollectionViewCellmiddle
objCollectionViewCellMiddle.lblMiddle.text = top[indexPath.row] as? String
return objCollectionViewCellMiddle
}
现在,TableView 显示了两个 CollectionView,但它在两个 CollectionView 中打印来自 top 数组的相同数据
我想要:
第一个 CollectionView ---> 显示 top 数组
第二个CollectionView ---> 显示bottom 数组
我玩弄了 indexPath.section,但无法让它工作,因为它总是打印“0”
【问题讨论】:
-
这是因为 uitableview 正在缓存单元格作为性能优化的一部分。尝试为 UItableViewCells 使用不同的单元格标识符。例如cellmiddle1 和 cellmiddle2
-
@firstinq 这意味着我必须从情节提要中添加一个新单元格?
-
您的 indexPath.section 将始终为 0,因为它只有一个 tableview 部分和每个 collectionview 一个部分
标签: ios swift uitableview uicollectionview uicollectionviewcell