0

我创建了一个collectionView,并使它的委托符合UICollectionViewDelegateFlowLayout附加到主ViewController的协议,以便修改UICollectionView's单元格的某些方面。最初我是这样做的:

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.delegate = self 

}

然后我意识到这不起作用,因为有一个错误,所以我修复了它:

 override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.delegate = self as? UICollectionViewDelegateFlowLayout

}

问题是我尝试在ViewController中实现的每个方法在运行时似乎都不起作用。

我在这里实现了这个方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: collectionView.frame.width/3, height: collectionView.frame.width/3)

}

但它在应用程序运行期间不起作用。

4

1 回答 1

0

“所以我把它修好了”

你让错误消失了,但你没有修复它。您需要通过将视图控制器类UICollectionViewDelegateFlowLayout添加到其类声明中来使视图控制器类实际采用,如下所示:

class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout {

或将其添加到视图控制器的扩展中,如下所示:

extension ProfileViewController: UICollectionViewDelegateFlowLayout {
于 2019-11-17T23:05:35.000 回答