2016-11-07 93 views
1

此代码用于在swift 2.3和更早版本中工作。但是当我在Swift 3中更新它时,应用程序崩溃了。layoutAttributesForElementsInRect Swift 3(UICollectionView)

这是SWIFT 2.3

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? 
{ 
    var layoutAttributes = [UICollectionViewLayoutAttributes]() 
    layoutInfo?.enumerateKeysAndObjectsUsingBlock({ (object: AnyObject, elementInfo: AnyObject, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
     let infoDic = elementInfo as! NSDictionary as NSDictionary! 
     infoDic.enumerateKeysAndObjectsUsingBlock({ (object: AnyObject!, attributes: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
      let attr = attributes as! UICollectionViewLayoutAttributes 
      if (CGRectIntersectsRect(rect, attributes.frame)) 
      { 
       layoutAttributes.append(attr) 
      } 

     }) 
    }) 

    return layoutAttributes 
} 

的代码,这是更新版本雨燕3

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

    var layoutAttributes = [UICollectionViewLayoutAttributes]() 
    layoutInfo?.enumerateKeysAndObjects({ (object: AnyObject, elementInfo: AnyObject, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
     let infoDic = elementInfo as! NSDictionary as NSDictionary! 
     infoDic?.enumerateKeysAndObjects({ (object: AnyObject!, attributes: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 

      let attr = attributes as! UICollectionViewLayoutAttributes 
      if (rect.intersects(attributes.frame)) 
      { 
       layoutAttributes.append(attr) 
      } 

     } as! (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Void) 
    } as! (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Void) 

    return layoutAttributes 
} 

我得到这个崩溃在}作为! (Any,Any,UnsafeMutablePointer) - > Void)EXC_BREAKPOINT

任何人都可以帮我吗?

这是我从这个家伙拿来的项目。 https://github.com/CoderXpert/EPGGrid

回答

1

我认为你已经管理了某种方式将自己与这个主题结在一起。 (如果我最终在Swift代码中使用了大量的cast和ObjectiveC类型,那么对我来说总是一个警告标志)。我刚刚看了一下layoutAttributesForElements(中)在我的应用程序之一的实施,并把它归结为以下几点:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

    var layoutAttributes = [UICollectionViewLayoutAttributes]() 
    if cache.isEmpty { 
     self.prepare() 
    } 
    for attributes in cache { 
     if attributes.frame.intersects(rect) { 
      layoutAttributes.append(self.layoutAttributesForItem(at: attributes.indexPath)!) 
     } 
    } 
    return layoutAttributes 
} 

在此实现,高速缓存是准备UICollectionViewLayoutAttributes的数组时集合视图被初始化或数据改变(通过prepare()方法)。在确保缓存非空之后,您需要做的就是遍历缓存并收集其帧与所讨论帧相交的任何属性。如果捕获到属性,则使用方法layoutAttributesForItemAt(另一个基本功能,如果您是布局管理器的子类)来拉入所需的值。

尽管很难确切知道代码中发生了什么,但我认为问题在于layoutInfo和infoDic的结构方式不适合手边的任务(它们是字典吗?),因此编码体操尝试获得结果!无论如何,希望有所帮助。

+0

是的,他们是字典..有一种方法,我可以用我的layoutInfo和infoDic填充缓存? – jboisjoli

相关问题