Ios 如何在动态uitableview中创建动态uicollectionview
我是swift和iOS开发的新手。我想在swift中创建包含集合视图的tableview。到目前为止,我能够做到这一点,但当集合视图中的项目数和tableview中的行数取决于我正在检索的json数据时,问题就出现了。我无法在“收藏”视图中对项目数进行编程。我正在为tableviewcell和collectionviewcell使用自定义单元格类。如何以不同方式更改表的collectionview单元格中每个表行的数据。集合视图委托的代码Ios 如何在动态uitableview中创建动态uicollectionview,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我是swift和iOS开发的新手。我想在swift中创建包含集合视图的tableview。到目前为止,我能够做到这一点,但当集合视图中的项目数和tableview中的行数取决于我正在检索的json数据时,问题就出现了。我无法在“收藏”视图中对项目数进行编程。我正在为tableviewcell和collectionviewcell使用自定义单元格类。如何以不同方式更改表的collectionview单元格中每个表行的数据。集合视图委托的代码 func collectionView(collecti
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
}
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
}
tableview代理的代码
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
}
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的。数据源是抽象的,每个单元都有自己的数据源。看看这个,你应该能够解决你的问题 在这种情况下,使用集合视图肯定比表视图有一些优势。其他有用的链接: 教程: 源代码:
希望有帮助 集合视图在表行中?是集合视图在表行中,包含图像,labelcellforrowatndexpath仅返回单元格nothing其他func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell{let cell=self.tableView.dequeueReusableCellWithIdentifier(“tablecell”,forIndexPath:indexPath)返回单元格}