I have a UICollectionView
inside a UITableView
.
Just like this image
There are 3categories and under each category, there is some content. I did this using tag value. But if I want dynamic category, then how to populate?
Here is my code:
import UIKit
class ViewController: UIViewController {
var categories = ["Revitalize", "Focus","Motivation"]
var revitalImages: [UIImage] = [UIImage(named: "revitalize1.png")!, UIImage(named: "revitalize2.png")!, UIImage(named: "revitalize3.png")!, UIImage(named: "revitalize4.png")!]
var focusImages: [UIImage] = [UIImage(named: "focus1.png")!, UIImage(named: "focus2.png")!, UIImage(named: "focus3.png")!, UIImage(named: "focus4.png")!]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
}
override open var shouldAutorotate: Bool {
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
// tableView.estimatedRowHeight = 200
// tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200
tableView.estimatedSectionHeaderHeight = 75
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
}
extension ViewController : UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 0{
return 4
}
if collectionView.tag == 1{
return 4
}
if collectionView.tag == 2{
return 3
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
if collectionView.tag == 0{
cell.imageView.image = revitalImages[indexPath.row]
}
if collectionView.tag == 1{
cell.imageView.image = focusImages[indexPath.row]
}
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
cell.collectionView.tag = indexPath.row
cell.collectionView.reloadData()
cell.titleLbl.text = categories[indexPath.row]
cell.contentView.backgroundColor = UIColor.white
cell.backgroundColor = UIColor.white
return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
return headerView
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
if let headerTitle = view as? UITableViewHeaderFooterView {
headerTitle.textLabel?.textColor = UIColor.clear
}
}
}
I want to show data dynamically without using tag 0,1,2,.....