8

我的 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))

}

谢谢,

4

3 回答 3

7

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

于 2017-01-29T23:34:45.927 回答
4

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

collectionView.delegate = self
于 2018-10-02T10:34:18.923 回答
0

我设法通过调用解决了同样的问题

collectionView.delegate = self

collectionView.dataSource = self

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

于 2021-06-04T08:56:36.987 回答