2017-01-12 11 views
1

コレクションビューのセルをテーブルビューのセル内に実装していますが、コレクションビューのindexPathで奇妙なことが起こっています。UITableViewCellのSwift UICollectionView indexPathエラー

numberOfitemsInSectionが2の場合、numberOfItemsinSectionは3回、cellForItemAtは1回だけ発生します。コレクションビューには1つのセクションと2つのセルが必要です。ここ

は、ソースデータである:ここ

[["myDomain.jpg1", "myDomain.jpg2"]] 

次のコードからのコンソール出力である:ここ

"number of items in section: 2" 
"number of items in section: 2" 
"number of items in section: 2" 
"cell for item at [0, 0]" 
"indexPath.section: 0"  
"indexPath.row: 0"  
"myDomain.jpg1" 

は私のビューコントローラである:ここ

class WishListsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var tableView: UITableView = UITableView() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate  = self 

    tableView.dataSource = self 

    tableView.register(WishListsCell.self, forCellReuseIdentifier: cellId) 

    self.view.addSubview(tableView) 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return wishLists.count // 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

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

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WishListsCell 

    return cell 
} 

は私です表ビューセル:

class WishListsCell: UITableViewCell { 

var collectionView: UICollectionView! 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
    layout.itemSize = CGSize(width: screenWidth, height: (screenWidth/4)) 
    layout.minimumInteritemSpacing = 0 
    layout.minimumLineSpacing = 0 
    layout.estimatedItemSize.height = (screenWidth/4) 
    layout.estimatedItemSize.width = screenWidth 

    collectionView = UICollectionView(frame: contentView.frame, collectionViewLayout: layout) 

    collectionView.delegate = self 

    collectionView.dataSource = self 

    collectionView.register(WishListsCollectionViewCell.self, forCellWithReuseIdentifier: cellId) 

    self.contentView.addSubview(collectionView) 

} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
}  
} 

extension WishListsCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    let items = wishListUrls[section] 

    debugPrint("number of items in section: \(items.count)") 

    return items.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WishListsCollectionViewCell 

    debugPrint("cell for item at \(indexPath)") 

    debugPrint("indexPath.section: \(indexPath.section)") 

    debugPrint("indexPath.row: \(indexPath.row)") 

    if let imageUrl = wishListUrls[indexPath.section][indexPath.row] as String! { 

     debugPrint(imageUrl) 
+2

を使用します。https:// ashfurrowここにあなたがTableViewCell内CollectionViewに取得することができ落とし穴についてのブログ記事です。 collectionview内の/ – muescha

+0

のコレクションビューでは、あなたは 'indexPath.row'の代わりに' indexPath.item'を使うべきです – muescha

+0

それは違いはありませんでした。 'indexPath.row'と' indexPath.item'は同義語であり、入れ替えることができると私は理解しています – markhorrocks

答えて

0

あなたのWishListsCell拡張でnumberOfSections(in collectionView)の実装が見つかりません。

あなたが投稿したスニペットに基づいているかどうかは確かではありませんが、そのように見えるかもしれません。

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return wishListUrls.count 
} 
+0

私はこれを実装しましたが、違いはありませんでした。私はそれがデフォルトに1と仮定し、私はそれが多くのコード例で見逃しているのを見ました。 – markhorrocks

+0

各テーブルビューセルには、1つのコレクションビューセクションと複数のセルが必要です。 – markhorrocks

+0

ええと...申し訳ありませんが、私はもっと助けになることはできません。これを実現するために必要なコードの量は、SOの投稿に少数の断片だけを理由に考えるのが難しくなります。 'debugPrint'ステートメントのうち約20個をもうけるつもりであるように見えます。ハハ。あなたはそれを得るだろうと確信しています。運が良かった。 –

0

最終的に最大の問題は、マスターとディテールがAPIから単一の要求としてはまだ私には利用できないよう5つのAlamofire要求から自分のデータをロードするに降りてきました。 tableView.reloadData()は、最初のAlamofireの完了時に明示的に起動するまで繰り返し実行されるようです。詳細データをローカル辞書として偽装すると、はるかにうまくいくように見えます。

とは別に、私は私はcollectionView.tag使用代わりindexPath.rowindexPath.sectionした唯一の変化は、私は1つの行で各私の表のセル内の複数のセクションを有していることであった。このSwift - how to open another viewcontroller with CollectionViewCell inside UITableViewCell

ようにそれを実装します。

は、私はまた、サイドノートとして

https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/この

https://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/

関連する問題