2016-09-09 100 views
0

我將我的項目從快捷2迅速3,我有困難理解什麼是錯用下面的函數:layoutAttributesForElements函數給出錯誤警告

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
    var attributes = [UICollectionViewLayoutAttributes]() 
    if self.itemAttributes != nil { 
     for section in self.itemAttributes { 

      let filteredArray = (section as AnyObject).filtered(

       using: NSPredicate { (evaluatedObject, bindings) -> Bool in 
        return rect.intersects(evaluatedObject.frame) 
       } 
      ) as! [UICollectionViewLayoutAttributes] 

      attributes.append(contentsOf: filteredArray) 

     } 
    } 

    return attributes 
} 

self.itemAttributes是定義爲一個類屬性:

var itemAttributes : NSMutableArray!

sectionAttributes被設置在另一個函數:

override func prepare() { 
    if self.collectionView?.numberOfSections == 0 { 
     return 
    } 

    if (self.itemAttributes != nil && self.itemAttributes.count > 0) { 
     for section in 0..<self.collectionView!.numberOfSections { 
      let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section) 
      for index in 0..<numberOfItems { 
       if section != 0 && index != 0 { 
        continue 
       } 

       let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))! 
       if section == 0 { 
        var frame = attributes.frame 
        frame.origin.y = self.collectionView!.contentOffset.y 
        attributes.frame = frame 
       } 

       if index == 0 { 
        var frame = attributes.frame 
        frame.origin.x = self.collectionView!.contentOffset.x 
        attributes.frame = frame 
       } 
      } 
     } 
     return 
    } 

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) { 
     self.calculateItemsSize() 
    } 

    var column = 0 
    var xOffset : CGFloat = 0 
    var yOffset : CGFloat = 0 
    var contentWidth : CGFloat = 0 
    var contentHeight : CGFloat = 0 

    for section in 0..<self.collectionView!.numberOfSections { 
     let sectionAttributes = NSMutableArray() 

     for index in 0..<numberOfColumns { 
      let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue 
      let indexPath = IndexPath(item: index, section: section) 
      let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
      attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral 

      if section == 0 && index == 0 { 
       attributes.zIndex = 1024; 
      } else if section == 0 || index == 0 { 
       attributes.zIndex = 1023 
      } 

      if section == 0 { 
       var frame = attributes.frame 
       frame.origin.y = self.collectionView!.contentOffset.y 
       attributes.frame = frame 
      } 
      if index == 0 { 
       var frame = attributes.frame 
       frame.origin.x = self.collectionView!.contentOffset.x 
       attributes.frame = frame 
      } 

      sectionAttributes.add(attributes) 

      xOffset += (itemSize?.width)! 
      column += 1 

      if column == numberOfColumns { 
       if xOffset > contentWidth { 
        contentWidth = xOffset 
       } 

       column = 0 
       xOffset = 0 
       yOffset += (itemSize?.height)! 
      } 
     } 
     if (self.itemAttributes == nil) { 
      self.itemAttributes = NSMutableArray(capacity: self.collectionView!.numberOfSections) 
     } 
     self.itemAttributes .add(sectionAttributes) 
    } 

    let attributes : UICollectionViewLayoutAttributes = (self.itemAttributes.lastObject as AnyObject).lastObject as! UICollectionViewLayoutAttributes 
    contentHeight = attributes.frame.origin.y + attributes.frame.size.height 
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)} 

Xcode中拋出一個Type of expression is ambiguous without more contextreturn rect.intersects(evaluatedObject.frame)

+0

你的'itemAttributes'(或'sectionAttributes')包含什麼?這是你問題的關鍵部分。您需要儘可能精確地描述「itemAttributes」和「sectionAttributes」。 – OOPer

+0

請更新您的問題並使其成爲可重現問題的代碼。 – OOPer

+0

我正在更新這個問題,評論中的代碼片段很難理解 –

回答

5

layoutAttributesForElements功能在夫特3,值類型的NSMutableArray(或NSArray以及)已成爲Any,這與工作一個很難的事情。

您最好儘可能快地將您的數組類型轉換爲Swift數組。

試試這個:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
    var attributes = [UICollectionViewLayoutAttributes]() 
    if let itemAttributes = self.itemAttributes as NSArray as? [[UICollectionViewLayoutAttributes]] { 
     for section in itemAttributes { 
      let filteredArray = section.filter {evaluatedObject in 
       return rect.intersects(evaluatedObject.frame) 
      } 
      attributes.append(contentsOf: filteredArray) 
     } 
    } 
    return attributes 
} 

但我建議你用斯威夫特本地Array S或Dictionary S作爲遠,你可以。

var itemAttributes: [[UICollectionViewLayoutAttributes]] = [] 

     var sectionAttributes: [UICollectionViewLayoutAttributes] = [] 

(有了這個,你可能需要一些更多的解決方法,但你很快就會發現他們。)

+0

是的,這有助於讓錯誤消失。謝謝! –

0

隨着OOPer的幫助下,我做了如下修改代碼:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
    var attributes = [UICollectionViewLayoutAttributes]() 
    if let itemAttributes = self.itemAttributes { 
     for section in itemAttributes { 
      let filteredArray = section.filter{ evaluatedObject in 
       return rect.intersects(evaluatedObject.frame) 
       } 
      attributes.append(contentsOf: filteredArray) 
     } 
    } 

    return attributes 
} 

override func prepare() { 
    if self.collectionView?.numberOfSections == 0 { 
     return 
    } 

    if (self.itemAttributes.count > 0) { 
     for section in 0..<self.collectionView!.numberOfSections { 
      let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section) 
      for index in 0..<numberOfItems { 
       if section != 0 && index != 0 { 
        continue 
       } 

       let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))! 
       if section == 0 { 
        var frame = attributes.frame 
        frame.origin.y = self.collectionView!.contentOffset.y 
        attributes.frame = frame 
       } 

       if index == 0 { 
        var frame = attributes.frame 
        frame.origin.x = self.collectionView!.contentOffset.x 
        attributes.frame = frame 
       } 
      } 
     } 
     return 
    } 

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) { 
     self.calculateItemsSize() 
    } 

    var column = 0 
    var xOffset : CGFloat = 0 
    var yOffset : CGFloat = 0 
    var contentWidth : CGFloat = 0 
    var contentHeight : CGFloat = 0 

    for section in 0..<self.collectionView!.numberOfSections { 
     var sectionAttributes: [UICollectionViewLayoutAttributes] = [] 

     for index in 0..<numberOfColumns { 
      let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue 
      let indexPath = IndexPath(item: index, section: section) 
      let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
      attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral 

      if section == 0 && index == 0 { 
       attributes.zIndex = 1024; 
      } else if section == 0 || index == 0 { 
       attributes.zIndex = 1023 
      } 

      if section == 0 { 
       var frame = attributes.frame 
       frame.origin.y = self.collectionView!.contentOffset.y 
       attributes.frame = frame 
      } 
      if index == 0 { 
       var frame = attributes.frame 
       frame.origin.x = self.collectionView!.contentOffset.x 
       attributes.frame = frame 
      } 

      sectionAttributes.append(attributes) 

      xOffset += (itemSize?.width)! 
      column += 1 

      if column == numberOfColumns { 
       if xOffset > contentWidth { 
        contentWidth = xOffset 
       } 

       column = 0 
       xOffset = 0 
       yOffset += (itemSize?.height)! 
      } 
     } 
     self.itemAttributes.append(sectionAttributes) 
    } 

    if let attributes = self.itemAttributes.last?.last { 
     contentHeight = attributes.frame.origin.y + attributes.frame.size.height 
     self.contentSize = CGSize(width: contentWidth, height: contentHeight) 
    } 
} 

var itemAttributes : [[UICollectionViewLayoutAttributes]]!