gpt4 book ai didi

ios - 如何在动态 uitableview 中创建动态 uicollectionview

转载 作者:行者123 更新时间:2023-11-30 13:47:08 25 4
gpt4 key购买 nike

我是 swift 和 iOS 开发的新手。我想在 swift 中创建包含 Collection View 的 TableView 。到目前为止我可以做到,但是当 Collection View 中的项目数量没有时,我的问题就出现了。 tableview 中的行数取决于我正在检索的 json 数据。我无法在 Collection View 中对项目数量进行编程。我正在为 tableviewcell 和 collectionviewcell 使用自定义单元格类。我如何以不同方式更改表的 CollectionView 单元格中的数据,每个表行不同。 Collection View 委托(delegate)的代码

func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {

return array_servicelist.objectAtIndex(something).valueForKey("subCategoryList")!.count
}

func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectioncell",
forIndexPath: indexPath) as! ServiceCollectionViewCell

let str :String = "\(array_servicelist.objectAtIndex(indexPath.section).valueForKey("subCategoryList")!.objectAtIndex(indexPath.item).valueForKey("phoneImageUrl") as! String)"
cell.serviceImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: str)!)!)
cell.serviceName.text = "\(array_servicelist.objectAtIndex(indexPath.section).valueForKey("subCategoryList")!.objectAtIndex(indexPath.item).valueForKey("categoryName") as! String)"

return cell
}

表格 View 委托(delegate)代码

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return array_servicelist.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tablecell", forIndexPath: indexPath)

return cell
}

func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {

guard let tableViewCell = cell as? ServiceTableViewCell else { return }

tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)

}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

最佳答案

这是 collectionViewCell 内的 collectionView 的示例,但这同样适用于 tableView。数据源是抽象的,每个单元都有自己的数据源。看到这里你应该能够找出你的问题。

https://github.com/irfanlone/Collection-View-in-a-collection-view-cell

在这种情况下,使用 Collection View 肯定比 TableView 有一些优势。其他有用的链接:

教程:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

源代码:https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell

希望有帮助!!

关于ios - 如何在动态 uitableview 中创建动态 uicollectionview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com