swift - UICollectionViewDelegateFlowLayout 方法未在 Swift 2.3 App 中调用

标签 swift delegates uicollectionviewdelegate

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

在我的 ViewDidLoad 方法中,我已将 Collection View 的委托(delegate)和数据源设置为 self.collectionView.delegate = selfself.collectionView.dataSource = self

这是我的委托(delegate)方法:

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 方法-

覆盖函数 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))

}

谢谢,

最佳答案

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

关于swift - UICollectionViewDelegateFlowLayout 方法未在 Swift 2.3 App 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920337/

相关文章:

ios - Swift - 标签未在 WatchKit 中从委托(delegate)回调中更新

macos - Swift 中的 OS X 应用程序 View 管理

c# - 应该使用委托(delegate)的一些常见场景是什么?

objective-c - iOS:释放正在使用的对象 (ARC)

ios - UICollectionViewCell 变为隐藏=YES

ios - 如果 cellForItemAtIndexPath : is called in didSelectItemAtIndexPath: 有什么问题

ios - Firebase - 推送通知 - 不工作

ios - 注册/登录后,所有 VC 上都有一个侧边栏(如 Facebook)

cocoa - 从 NSFontPanel 过滤字体?

ios - CollectionView 默认项目选择