【问题标题】:Assign different data and different length of items to a UICollectionView inside a UITableView将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView
【发布时间】: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


    【解决方案1】:

    您只需重新设计您的 tableviewCell 添加一个标签并在标签下添加 collectionview 一旦您将数据添加到 collectionview 中,您只需将类别名称添加到标签,所有子类别都将在该标签下的 collectionview 中。

    希望你能理解我试图告诉你的内容,如果你不问的话。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2016-11-03
      相关资源
      最近更新 更多