gpt4 book ai didi

ios - layoutAttributesForElementsInRect Swift 3(UICollectionView)

转载 作者:行者123 更新时间:2023-11-28 06:30:38 27 4
gpt4 key购买 nike

此代码用于 swift 2.3 及更早版本。但是当我在 Swift 3 中更新它时,应用程序崩溃了。

这是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
}

这是 Swift 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

最佳答案

我认为您已经设法以某种方式将自己束缚在这个话题上。 (如果我最终在 Swift 代码中使用大量转换和 ObjectiveC 类型,这对我来说总是一个警告信号)。我刚刚在我的一个应用程序中查看了 layoutAttributesForElements(in) 的实现,它归结为以下内容:

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
}

在此实现中,缓存是在 Collection View 初始化或数据更改(通过 prepare() 方法)时准备的 UICollectionViewLayoutAttributes 数组。确保缓存非空后,您需要做的就是遍历缓存并收集帧与相关帧相交的所有属性。如果捕获到属性,请使用 layoutAttributesForItemAt 方法(如果您正在对布局管理器进行子类化,则这是另一个基本函数)来提取所需的值。

虽然很难确切地看到您的代码中发生了什么,但我认为问题在于 layoutInfo 和 infoDic 的结构方式不适合手头的任务(它们是字典吗?),因此编码体操尝试并获得结果!不管怎样,希望对您有所帮助。

关于ios - layoutAttributesForElementsInRect Swift 3(UICollectionView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com