【问题标题】:How can i create dynamic uicollectionview within a dynamic uitableview如何在动态 uitableview 中创建动态 uicollectionview
【发布时间】:2016-01-14 09:34:04
【问题描述】:

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

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
}

tableview 委托的代码

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
}

【问题讨论】:

  • 集合视图在表格行内?
  • 是的,集合视图在表格行中,包含图像和标签
  • cellforrowatndexpath 只返回单元格 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("tablecell", forIndexPath: indexPath) 返回单元格}

标签: ios swift uicollectionview


【解决方案1】:

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

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

在这种情况下,使用集合视图肯定比表格视图有一些优势。其他有用的链接:

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

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

希望有帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2013-08-12
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多