【问题标题】:UICollectionViewDelegateFlowLayout methods not called in Swift 2.3 AppSwift 2.3 App 中未调用 UICollectionViewDelegateFlowLayout 方法
【发布时间】:2017-01-29 11:23:31
【问题描述】:

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

}

谢谢,

【问题讨论】:

  • 您是否设置了断点并确保它们不被调用?除非你这样做了,否则你的开关可能只是进入默认情况。
  • 发布整个viewDidLoad方法
  • @BenjaminLowry 是的,我确实设置了断点。谢谢,
  • @Alistra ViewDidLoad 添加。谢谢

标签: swift delegates uicollectionviewdelegate


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    collectionView.delegate = self
    

    【讨论】:

      【解决方案3】:

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

      collectionView.delegate = self
      

      之前

      collectionView.dataSource = self
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多