简体   繁体   中英

Swift3 iOS -How to get Data from All IndexPaths from TableView Before Cell Is Pressed

tableView内部的collectionView

I followed Ash Furrows tutorial collectionView inside tableView

I have TableItem class that has a name property and an array of URLs. I have a tableView that has collectionView inside of it.

I vertically populate my tableview with the whatever amount of TableItems are available and I horizontally populate my collectionView with however many amount of urls that are inside the array from the TableItem class.

I'm not concerned with pressing a table cell and then getting the information, I want the data to be displayed simultaneously in both collections.

How can I pull the TableItem data and display it in the collectionView at the same time as the tableView?

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

 }

Like this:

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

Should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM