2016-01-14 44 views
0

我是swift和iOS开发的新手。我想在swift中创建包含集合视图的tableview。我能够做到目前为止,但我的问题出现时收集视图中的项目数量和没有。 tableview中的行取决于我检索的json数据。我无法在集合视图中编辑项目数量。我为tableviewcell和collectionviewcell使用自定义单元类。如何更改数据在表中的的CollectionView小区中的每个表行differently.code收集视图代表如何在动态的uitableview中创建动态的uicollectionview

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 
} 
+0

集合视图在表格行内吗? –

+0

是集合视图是表行,包含图像和标签 –

+0

cellforrowatndexpath是只返回细胞没有别的 FUNC的tableView中(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - > {UITableViewCell中让 细胞= self.tableView.dequeueReusableCellWithIdentifier (“tablecell”,forIndexPath:indexPath) return cell } –

回答

0

这是一个collectionViewCell内部的CollectionView的一个例子,但同样适用于tableView。数据源被抽象出来,每个单元都有自己的数据源。你应该能够弄清楚你的问题。

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

使用集合观点有一定超过在这种情况下tableviews一定的优势。其他有用的链接:

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

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

希望帮助!

相关问题