简体   繁体   English

在 TableView 内的 CollectionView 中填充动态数据

[英]Populating dynamic data in CollectionView which is inside TableView

Main View Controller主视图控制器

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return CategoryNames.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! CategoryTableViewCell
    cell.lblCategoryName.text = CategoryNames[indexPath.row] as? String
    cell.ViewCategogyBackground.layer.cornerRadius=20
    cell.ViewCategogyBackground.layer.maskedCorners = [.layerMaxXMinYCorner]
    
    cell.arrayForCollectionView = [["xx","yy"],["zz","vv","tt"],["hello"],["11","44"]]
    cell.reloadCollectionView()
    return cell
}

Table View Cell表格视图单元格

var arrayForCollectionView : [[String]]!

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)       -> Int {
           if (arrayForCollectionView != nil) {
               return arrayForCollectionView.count
           }
           return 0
       }

       func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryItemCollectionViewCell
             
           

           let rowValue = arrayForCollectionView[indexPath.row]
           for i in 0..<rowValue.count {
               cell.lblItemName.text = rowValue[i] as? String
           }
           return cell
       }

Output here is my output I want to display data like first section of 2D array under t1(Category 1) and second section under t2(category 2).这里的输出是我的输出,我想在 t1(类别 1)下显示数据,如二维数组的第一部分和 t2(类别 2)下的第二部分。 both categories and items in collection view are dynamic集合视图中的类别和项目都是动态的

You need to create a CollectionView inside TableView and create outlet of collectionView inside tableViewCell like this:您需要在 TableView 中创建一个 CollectionView 并在 tableViewCell 中创建 collectionView 的出口,如下所示:

class TimingSlotsTVCell: UITableViewCell
{
    @IBOutlet weak var timeSlotsCV: UICollectionView!
}

Now in your cellForRow of tableView do this:现在在 tableView 的 cellForRow 中执行以下操作:

let cell = tableView.dequeueReusableCell(withIdentifier: "TimingSlotsTVCell") as! TimingSlotsTVCell
cell.timeSlotsCV.reloadData()
return cell

Now collectionView cell will be called from inside tableView现在将从 tableView 内部调用 collectionView 单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM