【问题标题】:Swift3 iOS -How to get Data from All IndexPaths from TableView Before Cell Is PressedSwift iOS - 如何在按下单元格之前从 TableView 的所有 IndexPath 中获取数据
【发布时间】:2017-07-19 21:28:44
【问题描述】:

我跟着 Ash Furrows 教程 collectionView inside tableView

我有一个 TableItem 类,它有一个 name 属性和一个 URL 数组。我有一个 tableView,里面有 collectionView。

我用可用的任何数量的 TableItems 垂直填充我的 tableview,我用 TableItem 类的数组内的任何数量的 url 水平填充我的 collectionView。

我不关心按下表格单元格然后获取信息,我希望数据同时显示在两个集合中。

如何拉取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

 }

【问题讨论】:

    标签: ios swift uitableview uicollectionview nsindexpath


    【解决方案1】:

    像这样:

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

    应该可以。

    【讨论】:

    • 谢谢。我会在大约一个小时后尝试并回复您
    • 谢谢,但这不起作用,因为 collectionView 需要项目的数量。
    • 对于项目的数量使用这个:let tableItem = tableItems[indexPath.section] as! TableItem returntableItem.urlsForCollection.count因为你想同时显示,我会说在 numberOfSections 你应该加载第一个项目并标记为选中。
    • 不,它不起作用。问题是 tableItems 已经在类级别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多