首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >layoutAttributesForElementsInRect Swift 3 (UICollectionView)

layoutAttributesForElementsInRect Swift 3 (UICollectionView)
EN

Stack Overflow用户
提问于 2016-11-07 21:41:12
回答 1查看 4.5K关注 0票数 1

这段代码曾经在swift 2.3和更早的版本中工作过。但当我在Swift 3中更新它时,应用程序崩溃了。

这是swift 2.3中的代码

代码语言:javascript
运行
AI代码解释
复制
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更新的版本

代码语言:javascript
运行
AI代码解释
复制
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) -> EXC_BREAKPOINT )

有人能帮我吗?

这是我从这个人那里接来的项目。https://github.com/CoderXpert/EPGGrid

EN

回答 1

Stack Overflow用户

发布于 2016-11-08 14:00:20

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

代码语言:javascript
运行
AI代码解释
复制
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的结构方式不适合手头的任务(它们是字典吗?),因此需要编写代码来尝试并获得结果!不管怎样,希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40475160

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文