简体   繁体   English

Swift3 iOS-如何在按下单元格之前从TableView的所有IndexPaths获取数据

[英]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 在tableView中关注了Ash Furrows教程collectionView

I have TableItem class that has a name property and an array of URLs. 我有具有名称属性和URL数组的TableItem类。 I have a tableView that has collectionView inside of it. 我有一个tableView里面有collectionView。

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. 我用可用的任意数量的TableItem垂直填充tableview,然后用TableItem类的数组内部的任意数量的URL水平填充collectionView。

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? 如何提取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

 }

Like this: 像这样:

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

Should work. 应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift3中的tableview单元格 - tableview cell in swift3 如何从按钮按下事件中获取单元格数据(自定义表格视图单元格) - How to get cell data from button pressed event (Custom tableview cell) 如何在Swift3中将数据从所有TableViewCell传递到CollectionView - How to pass data from all TableViewCell to CollectionView in Swift3 如何从 ios swift3 中的 CoreData 获取特定范围的数据 - How to fetch particular range data from CoreData in ios swift3 如何在Swift3中从另一个类获取数据 - How to Get data from another class in swift3 将数据从自定义单元格(在ViewController的表中)传递到ViewController本身Swift3 iOS - pass data from custom cell(in table which is on ViewController) to the viewcontroller itself Swift3 iOS 如何使用Swift3从Firestore集合中使用检索到的数据填充TableView - How to populate a TableView with retrieved data from a Firestore collection using Swift3 iOS Swift-从选定的TableView单元格获取BackgroundColor - iOS Swift - Get BackgroundColor from selected TableView Cell 如何将图像从 Web 服务上传到 Swift3 的 tableview? - How to upload image from web service to tableview for Swift3? 在Swift中以不同的间隔刷新TableView单元格以从URL获取数据 - Refresh TableView Cell at different intervals in Swift to get data from URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM