【发布时间】: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