2017-07-19 3 views
0

collectionView inside tableViewSwift3 iOSの - どのようには、Cellの前にテーブルビューからすべてのIndexPathsからデータを取得するために

を押されて、私はアッシュ畝チュートリアルcollectionView inside tableView

を追った私は、nameプロパティとURLの配列を持つ表項目のクラスを持っています。私はそれの中にcollectionViewを持っているtableViewを持っています。

私はTableItemsが利用可能な任意の量で私のテーブルビューを垂直に設定し、TableItemクラスの配列内にある多くの量のURLを使って、私のコレクションビューを水平に取り込みます。

テーブルセルを押して情報を取得するのではなく、データを両方のコレクションに同時に表示したいと思っています。

TableViewと同時にTableItemデータを取得してコレクションビューに表示するにはどうすればよいですか?このよう

class TableItem{ 
    var name: String? 
    var urlsForCollection: [URl] = [] 
} 

var tableItems = [TableItem]() 

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

    return tableItems.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell 

    cell.textLabel.text = tableItems[indexPath.row].name 

    return cell 
} 


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

    //how do I return the urlsForCollection.count from each TableItem in tableItems 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell 

    //how can I access the urlsForCollection for each TableItem in tableItems 
    for url in urlsForCollection{ 
      imageView.sd_setImage(with: url!) 
    } 

    return cell 

} 

答えて

0

let tableItem = tableItems[indexPath.row] as! TableItem 
for url in tableItem.urlsForCollection{ 
     imageView.sd_setImage(with: url!) 
} 

が動作するはずです。

+0

ありがとうございました。私は約1時間でそれを試してみると、あなたに戻ってきます –

+0

ありがとうございますが、collectionViewにはアイテムの量が必要なので動作しません。 –

+0

アイテムの量については、これを使用してください: 'let tableItem = tableItems [indexPath.section] as! TableItem returntableItem.urlsForCollection.count' 両方を同時に表示したいので、numberOfSectionsには最初の項目をロードし、選択済みにする必要があります。 – ppinho

関連する問題