【发布时间】:2019-08-24 08:34:50
【问题描述】:
我有一个类别列表,每个类别都有一个子类别,例如类别一可能有 10 个子类别,类别二可能有 5 个子类别。
我在 UITableView 中实现了一个 UICollectionView,它运行良好,但唯一的问题是如何在特定类别中显示子类别
我有这些课程
这是我的CategoriesCollection 课程
class CategoriesCollection: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate {
override func awakeFromNib() {
self.delegate = self
self.dataSource = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//horizontal items number
return imageData.count //this should be the subcategories count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "subcategoriesCell", for: indexPath) as! SubcategoriesCollectionCell
cell.subcategoryImg.image = imageData[indexPath.row]
cell.subcategoryName.text = imageName[indexPath.row]
return cell
}
}
这里是我的表格视图类
class HomeTable: UITableView,UITableViewDelegate,UITableViewDataSource{
override func awakeFromNib() {
self.delegate = self
self.dataSource = self
}
let cellSpacingHeight: CGFloat = 0
func numberOfSections(in tableView:UITableView)->Int{
return category.count
}
func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int)->Int{
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
func tableView(_ tableView:UITableView,cellForRowAt indexPath: IndexPath)->UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell") as! CategoryCell
//setting the category title here
return cell
}
}
【问题讨论】:
标签: ios swift uitableview uicollectionview