简体   繁体   中英

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). 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:

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

Now in your cellForRow of tableView do this:

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

Now collectionView cell will be called from inside tableView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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