从 API 填充 TableView 中的 CollectionView

阿曼·乔拉

我正在使用 alamofire 在模型类的帮助下从数组内的 API 存储中获取数据。现在我想在 TableView 内的 CollectionView 中显示这些数据。因此,传递该数组以显示数据是一个问题,这里有一些代码,看看您是否能找到问题。

告诉我你是否想让我给你更多的信息

表格视图的单元格

if let cell = tableView.dequeueReusableCell(withIdentifier: "HeadlineRow", for: indexPath) as? HeadlineRow {

            let latest = self.latest[indexPath.section]
            cell.updateUI(latest: latest)

            return cell
        }

表视图单元类

var latestNews = [LatestNews]()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineCell", for: indexPath) as? HeadlineCell {

            let latest = self.latestNews[indexPath.row]
            cell.updateUI(latest: latest)

            return cell
        }

        return UICollectionViewCell()
    }

    func updateUI(latest: LatestNews) {
        self.latestNews.append(latest)
    }

集合视图类

func updateUI(latest: LatestNews){

        self.headlineImg.sd_setImage(with: URL(string: latest.thumbnail))
        headlineTextView.text = latest.headline
    }

所以问题基本上是我如何将数组从 MainViewController 转移到自定义 TableView 类到自定义集合视图类。

努里丁·塞利姆科

UITableViewCell 类:

class MyTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
{
 var data : [SomeData]?

@IBOutlet var collectionView: UICollectionView! // initialize your collectionview from storyboard

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellIdentifier", for: indexPath) as! MyCustomCollectionViewCell
    return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}
func initCollection()
{
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    self.collectionView.register(UINib(nibName:"MyCustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "myCellIdentifier")
}


func setData(data: [SomeData])
{
    self.data = data
    self.initCollection()
    self.collectionView.reloadData()
}
}

tableview 的 cellForRowAt:

let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellIdentifier", for: indexPath) as! MyTableViewCell
cell.setData(data: yourArray)

您还可以从 cellForRowAt 初始化“数据”数组,例如:

cell.data = yourArray

这是我为我的项目完成的示例,其中包含 tableviewcell 中的水平集合视图

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从collectionView的选定项中填充一个tableview

来自分类Dev

使用字典值在 tableView 中填充 collectionView 的标签

来自分类Dev

collectionView IOS中的tableView

来自分类Dev

在 javafx 中填充 tableview

来自分类Dev

在 collectionViewCell 中填充 tableView

来自分类Dev

在不同的 collectionView 单元格内填充不同的 tableView

来自分类Dev

REST API:无法填充Tableview

来自分类Dev

Tableview 中的 CollectionView,数据源错误

来自分类Dev

如何在collectionView中调用tableView的didDeselectRowAt

来自分类Dev

Google地方信息自动填充API不会在我的Tableview中填充确切的地址

来自分类Dev

从SQLite中的数据填充JavaFX中的TableView

来自分类Dev

如何从JSON在Swift 2中填充tableView?

来自分类Dev

使用Firestore中的数据填充TableView

来自分类Dev

按月顺序在TableView部分中填充对象

来自分类Dev

使用Swift在Xcode中填充TableView

来自分类Dev

在JavaFX的TableView中未填充列

来自分类Dev

未填充CollectionView吗?

来自分类Dev

快速过滤tableview数组时,tableview单元格中的collectionview不会刷新

来自分类Dev

无法填充在SceneBuilder 2中创建的JavaFX TableView

来自分类Dev

调用WCF服务后,在主UI线程中填充TableView

来自分类Dev

用Firebase数据填充tableview(数组未在.childAdded中更新)

来自分类Dev

从嵌套的plist数组(在字典内部)在tableview中填充NSDictionary

来自分类Dev

无法将数据填充到javafx中的TableView

来自分类Dev

如何在Swift 3中使用jSON填充TableView

来自分类Dev

使用数据库中的特定行填充 tableview

来自分类Dev

如何使用数组中的值填充 TableView 的列

来自分类Dev

使用 Sections 实现 TableView 并使用结构中的数据填充它

来自分类Dev

无法在侧边菜单中填充 tableview 单元格

来自分类Dev

TableView Controller中的iOS / AFNetworking TableView不显示从响应对象填充的数组中的任何数据

Related 相关文章

  1. 1

    从collectionView的选定项中填充一个tableview

  2. 2

    使用字典值在 tableView 中填充 collectionView 的标签

  3. 3

    collectionView IOS中的tableView

  4. 4

    在 javafx 中填充 tableview

  5. 5

    在 collectionViewCell 中填充 tableView

  6. 6

    在不同的 collectionView 单元格内填充不同的 tableView

  7. 7

    REST API:无法填充Tableview

  8. 8

    Tableview 中的 CollectionView,数据源错误

  9. 9

    如何在collectionView中调用tableView的didDeselectRowAt

  10. 10

    Google地方信息自动填充API不会在我的Tableview中填充确切的地址

  11. 11

    从SQLite中的数据填充JavaFX中的TableView

  12. 12

    如何从JSON在Swift 2中填充tableView?

  13. 13

    使用Firestore中的数据填充TableView

  14. 14

    按月顺序在TableView部分中填充对象

  15. 15

    使用Swift在Xcode中填充TableView

  16. 16

    在JavaFX的TableView中未填充列

  17. 17

    未填充CollectionView吗?

  18. 18

    快速过滤tableview数组时,tableview单元格中的collectionview不会刷新

  19. 19

    无法填充在SceneBuilder 2中创建的JavaFX TableView

  20. 20

    调用WCF服务后,在主UI线程中填充TableView

  21. 21

    用Firebase数据填充tableview(数组未在.childAdded中更新)

  22. 22

    从嵌套的plist数组(在字典内部)在tableview中填充NSDictionary

  23. 23

    无法将数据填充到javafx中的TableView

  24. 24

    如何在Swift 3中使用jSON填充TableView

  25. 25

    使用数据库中的特定行填充 tableview

  26. 26

    如何使用数组中的值填充 TableView 的列

  27. 27

    使用 Sections 实现 TableView 并使用结构中的数据填充它

  28. 28

    无法在侧边菜单中填充 tableview 单元格

  29. 29

    TableView Controller中的iOS / AFNetworking TableView不显示从响应对象填充的数组中的任何数据

热门标签

归档