将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView [英] Assign different data and different length of items to a UICollectionView inside a UITableView

查看:23
本文介绍了将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别列表,每个类别都有一个子类别,例如,类别一可能有 10 个子类别,类别二可能有 5 个子类别.

I have a category list and each category has a sub category say for example category one might have 10 subcategories and category two may have 5 subcategories.

我在 UITableView 中实现了一个 UICollectionView ,它工作得很好,但唯一的问题是我如何显示特定类别中的子类别我有这些课这是我的 CategoriesCollection

I have implemented an UICollectionView inside an UITableView which works perfect but the only problem is how can I display the subcategories inside a particular Category I have these classes this is my CategoriesCollection class

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

推荐答案

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

All you need is to redesign your tableviewCell add a label and under label add collectionview once you are adding data into collectionview you just need to add category name to the label and all subcategories will be in collectionview under that label like.

希望你能理解我想告诉你的,如果不随意问的话.

Hope you understand what i tried to tell you if not feel free to ask.

谢谢

这篇关于将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆