收集单元swift中的一行单元格?

时间:2017-08-16 22:12:15

标签: swift uicollectionview cell uicollectionviewcell

我有一个集合视图。我想让每个单元格以编程方式连续进行。这是我到目前为止所做的:

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.challenges.count
    }

    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell


        cell.myLabel.text = self.challenges[indexPath.item]
        cell.backgroundColor = .orange

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print("You selected cell #\(indexPath.item)!")
    }

}



class MyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var myLabel: UILabel!

}

知道接下来要做什么吗?  enter image description here

1 个答案:

答案 0 :(得分:0)

假设您希望每个单元格占据视图的整个宽度:

最简单的方法是,因为你已经将collectionView的委托设置为parentViewController,所以要实现以下函数:func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize并使其返回宽度为self.view.bound.width的大小。

此函数是UICollectionViewDelegateFlowLayout的一部分,使其工作的唯一要求是实现所述函数的类是collectionView的委托。

相关问题