Swift 2.3 App 中未调用 UICollectionViewDelegateFlowLayout 方法

UICollectionViewDelegateFlowLayout methods not called in Swift 2.3 App

None 个 UICollectionViewDelegateFlowLayout 方法被调用。有人可以帮我找到解决方案吗?

在我的 ViewDidLoad 方法中,我已将集合视图的委托和数据源设置为 self.collectionView.delegate = selfself.collectionView.dataSource = self

这是我的委托方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
    switch(deviceIdiom){
    case .Phone:

        return UIEdgeInsets(top: 50, left: 10, bottom: 0, right: 10)
    case .Pad:
        return UIEdgeInsets(top: 60, left: 20, bottom: 0, right: 20)
    default:
        break
    }
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return CGFloat(8)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let deviceIdiom =          UIScreen.mainScreen().traitCollection.userInterfaceIdiom

    switch(deviceIdiom){
    case .Phone:

        let cellWidth = (view.bounds.size.width - 16) / 2
        let cellHeight = cellWidth
     return CGSize(width: cellWidth, height: cellHeight)

    case .Pad:

        let cellWidth = (view.bounds.size.width - 40) / 2
        let cellHeight = cellWidth
        return CGSize(width: cellWidth, height: cellHeight)

    default:
        break
    }
}

我的 ViewDidLoad 方法-

覆盖 func viewDidLoad() { super.viewDidLoad()

 self.collectionView.dataSource = self
 self.collectionView.delegate = self
    cellImages = [
        "contact.png",
        "share.png",
        "setting.png",
        "logout.png"]
    cellLabels = [
        "Contact",
        "Share",
       "Setting",
        "Logout"]

    self.view.backgroundColor = uicolorFromHex(0xE5DADA)

    self.collectionView?.backgroundColor = uicolorFromHex(0xE5DADA)
    self.navigationController?.navigationBarHidden = false
    self.navigationItem.hidesBackButton = true
    navigationController!.navigationBar.titleTextAttributes =
        [NSForegroundColorAttributeName: UIColor.whiteColor()]
    navigationController!.navigationBar.barTintColor = UIColor(red: 0.0431, green: 0.0824, blue: 0.4392, alpha: 1.0)

    if let user = User.currentUser(){
    if(user.manager){
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Board", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(goToBoard))

}

谢谢,

我终于通过创建 class 的扩展解决了这个问题 MainViewController : UICollectionViewDelegateFlowLayout { } ,将所有委托方法放在那里并清理构建。它在 Swift 3 中工作 属性(在同一个 class 中),但对于 swift 2.3,由于某些奇怪的原因,它需要放在扩展中。谢谢,

对于任何来到这里的人来说,案例的另一个原因可能是没有设置代表。 只需检查您是否将 collectionView 委托分配给了自己。

collectionView.delegate = self

我通过致电

设法解决了同样的问题
collectionView.delegate = self

之前

collectionView.dataSource = self

(我有静态项目,从不重新加载数据)