user9266877
user9266877

Reputation:

create collectionView when tapped on a button

Hi , how can I make this using a collection view ? or when I tap on a button , It'll create a collectionView in my view controller . Actually I want to add a collection view but I want the number of it to be dynamically cause I'm getting it's count from a server . Please help me .

enter image description here

Upvotes: 0

Views: 346

Answers (1)

Kavitha Madhu
Kavitha Madhu

Reputation: 406

try this,I hope this will help you,

//viewcontroller.swift

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        return cell
    }

//Tableviewcell.swift

class TableViewCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{


    @IBOutlet var collectionview2: UICollectionView!
    @IBOutlet var collectionview1: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        collectionview1.dataSource = self
         collectionview1.delegate = self
         collectionview2.dataSource = self
         collectionview2.delegate = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if(collectionView == collectionview1){
            return 5
        }else{
            return 10
        }

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if(collectionView == collectionview1){
            let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
            return cell
        }else {
            let  cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
            return cell1
        }

    }


}

Finally set "collectionview1" scroll direction has Horizontal and set "collectionview2" scroll direction has Vertical.

Upvotes: 2

Related Questions