点击按钮时创建collectionView

时间:2018-09-06 11:25:37

标签: ios swift uicollectionview

您好,如何使用收藏夹视图进行此设置? 或者当我点击一个按钮时,它将在我的视图控制器中创建一个collectionView。 实际上,我想添加一个集合视图,但是我希望它的数量能够动态地增加,因为我正在从服务器获取它的计数。 请帮我 。

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试一下,希望对您有帮助

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

    }


}

最后将“ collectionview1”滚动方向设置为“水平”,将“ collectionview2”滚动方向设置为“垂直”。