2017-07-19 51 views
0

collectionView inside tableViewSwift3 iOS的 - 如何從所有IndexPaths從TableView中細胞是按下

之前我也跟着灰溝教程collectionView inside tableView

我有了一個name屬性和URL的數組表項類中獲取數據。我有一個tableView裏面有collectionView。

我垂直填充我的tableview與任何數量的TableItems是可用的,我水平填充我的collectionView與無論是從TableItem類數組內的數量。

我不關心按表格單元格然後獲取信息,我希望數據在兩個集合中同時顯示。

我怎樣才能拉TableItem數據,並在tableView同時顯示它在collectionView?

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

謝謝。我會在大約一小時內嘗試它,並回復 –

+0

謝謝,但這不起作用,因爲collectionView需要的項目數量。 –

+0

對於使用此項目的數量: 'let tableItem = tableItems [indexPath.section] as! TableItem returntableItem.urlsForCollection.count' 既然你想同時顯示,我會說在numberOfSections你應該加載第一個項目,並選擇標記。 – ppinho