2

下の図のように、各行に a がありますUITableViewUICollectionView

テーブルビュー内のコレクションビュー

ソース: 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
}
4

2 に答える 2

2

UICollectionView のインスタンスを持つカスタム UITableViewCell クラスがあると仮定しているので、cellForRowAtIndexPath を呼び出すときにセクション インデックスを渡すだけで済みます。

セクション インデックスを保持するには、tableViewCell クラスで var を作成する必要があります。

 class CustomTableViewCell: UITableViewCell {
      var sectionIndex:Int?

 }

それから cellForRow... を呼び出すときに、そのセルにセクションを渡すだけです。

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as CustomTableViewCell
    cell.sectionIndex = indexPath.section
    return cell
}

表示されていないため、コレクションビューにデータをロードする方法はわかりませんが、テーブルビューセルにセクションがあれば、データをロードするために多くのことを行うことができます.

詳細が必要な場合はお知らせください

于 2016-11-18T01:00:04.627 に答える