下の図のように、各行に a がありますUITableView
。UICollectionView
ソース: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
私のアプリの意図は、各テーブル ビュー行内に言語の文字セットを表示することです。各文字は、対応する行内のコレクション ビューのコレクション ビュー セル内に含まれています。
私が抱えている問題は、すべてのテーブルビュー行に英語の文字セットが表示されていることです。
これは、各 collectionview にはセクションが 1 つしかないため、すべての collectionview が同じindexPath.section
値 0 を使用するためです。
私がする必要があるのは、コレクションビューが含まれているテーブルビューセルのセクション値を取得し、何らかの方法でそれをに渡すことです
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
私はいくつかのことを試しましたが、コレクションビューからテーブルビューセクションの値にアクセスする方法が見つかりません。
私のコードは少し混乱していますが、一般的に重要な部分は
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
}