Dan*_*ram 2 uitableview uicollectionview swift swift3 indexpath
我在每行中都有一个UITableView
,UICollectionView
如下图所示。
来源:https : //ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
我的应用程序的目的是在每个表格视图行中显示一种语言的字符集。每个字符都包含在相应行内collectionview的collection view单元格内。
我遇到的问题是正在为每个tableview行显示英文字符集。
这是因为每个collectionview仅具有一个部分,因此每个collectionview使用相同indexPath.section
的零值。
我需要做的是获取collectionview所在的表视图单元格的section值,并以某种方式将其传递给
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几件事,但是找不到从其中的collectionview访问tableview部分值的方法。
我的代码有些混乱,但是通常重要的部分是
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath)
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//format inner collectionview cells
//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it?
cellCharLabel?.text = Languages.sharedInstance.alphabets[indexPath.section].set[indexPath.row].char
cellCharLabel?.textAlignment = .center
cellCharLabel?.font = UIFont(name: "Helvetica", size: 40)
cell.contentView.addSubview(cellCharLabel!)
return cell
}
Run Code Online (Sandbox Code Playgroud)
您可以设置collectionView
标签来制作它。
CollectionView
应该是的子视图tableViewCell
。那collectionView
可以是TableViewCell
您正在使用的自定义Seem 的属性Prototype Cell
。
class YourCustomizeTableViewCell: UITableViewCell {
let collectionView: CollectionView
...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as! YourCustomizeTableViewCell
cell.collectionView.tag = indexPath.row
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it?
cellCharLabel?.text = Languages.sharedInstance.alphabets[collectionView.tag].set[indexPath.row].char
...
return cell
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4365 次 |
最近记录: |