首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift collectionView添加货架装饰器

Swift collectionView添加货架装饰器
EN

Stack Overflow用户
提问于 2017-08-09 04:24:10
回答 1查看 444关注 0票数 0

我坚持在我的布局中以类似于架子的模式实现装饰视图。首先,我希望在滚动时生成新的decorationView元素,并且不将它们添加到缓存中。但是当我尝试用最后一个单元格的indexPath生成decorationView时,在删除项目时app立即崩溃。然后,我尝试为装饰视图实现一些缓存,在layoutAttributesForElements(in rect: CGRect)中检查它们,但是应用程序开始做奇怪的事情:如果我试图删除一开始没有出现在屏幕上的项目,应用程序立即崩溃。但是,如果我首先删除这些元素中的几个元素,然后从最后开始删除元素,那么一切都会正常进行。如何更好地将这些装饰器添加到我的布局中?我现在的代码如下:

代码语言:javascript
复制
class YourFoodFlowLayout: UICollectionViewFlowLayout {
private enum DecorationViewKind: String {
    case shelfView = "ShelfView"
}

private var cashedDecorationView = [UICollectionViewLayoutAttributes]()

override init() {
    super.init()
    self.register(ShelfCollectionReusableView.self, forDecorationViewOfKind: DecorationViewKind.shelfView.rawValue)
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.register(ShelfCollectionReusableView.self, forDecorationViewOfKind: DecorationViewKind.shelfView.rawValue)
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    guard let atributes = super.layoutAttributesForElements(in: rect) else {
        return nil
    }
    var mutatingAtributes = atributes
    //find max Y position of the cells

    var position = CGRect.zero
    position.size.width = rect.size.width
    position.size.height = 16

    for atribute in mutatingAtributes {
        atribute.zIndex = 1
        if atribute.frame.maxY > position.origin.y {
            position.origin.y = atribute.frame.maxY
            if rect.intersects(position) {
                var atribute: UICollectionViewLayoutAttributes? = nil
                for decorationView in cashedDecorationView {
                    if decorationView.frame == position {
                        atribute = decorationView
                    }
                }
                if let atribute = atribute {
                    mutatingAtributes.append(atribute)
                } else {
                    guard let shelfAtribute = layoutAttributesForDecorationView(ofKind: DecorationViewKind.shelfView.rawValue, at: IndexPath(index: cashedDecorationView.count)) else {
                        continue
                    }
                    shelfAtribute.frame = position
                    cashedDecorationView.append(shelfAtribute)
                    mutatingAtributes.append(shelfAtribute)
                }
            }

        }
    }

    return mutatingAtributes
}

override func prepare() {
    super.prepare()

}

override var collectionViewContentSize: CGSize {
    return CGSize(width: super.collectionViewContentSize.width, height: super.collectionViewContentSize.height + 16)
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    return super.layoutAttributesForItem(at: indexPath)
}


override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    switch elementKind {
    case DecorationViewKind.shelfView.rawValue:
        return UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, with: indexPath)
    default:
        print("Unexpected decoration layout element kind")
        return nil
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-09 22:14:27

好的,我发现了两件事: 1) layoutAttributesForElements(in rect: CGRect)不是每次滚动时都调用;2) collectionViewLayout有一些以前使用过的缓存indexPath。即使您尝试重新加载一些数据,也必须首先调用invalidateLayout()来重新加载这些indexPath。以下是对我有帮助的:

代码语言:javascript
复制
private var cachedDecorationView = [UICollectionViewLayoutAttributes]()
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        //Tip: layoutAtributes with the same indexPath may can not be allowed for reusing
        guard let atributes = super.layoutAttributesForElements(in: rect) else {
            return nil
        }
        var mutatingAtributes = atributes

        //find max Y position of the cells
        var position = CGRect.zero
        position.size.width = rect.size.width
        position.size.height = shelfSize

        for atribute in mutatingAtributes {
            atribute.zIndex = 1
            if atribute.frame.maxY > position.origin.y {
                position.origin.y = atribute.frame.maxY
                if rect.intersects(position) {
                    let shelf = prerareShelves(for: position)
                    mutatingAtributes.append(shelf)
                }

            }
        }

        return mutatingAtributes
    }

    func prerareShelves(for rect: CGRect) -> UICollectionViewLayoutAttributes {
        for cache in cachedDecorationView {
            if cache.frame == rect {
                return cache
            }
        }
        let indexForNewDecoration = Int(rect.origin.y/itemSize.height + shelfSize)
        let decoratorView = layoutAttributesForDecorationView(ofKind: DecorationViewKind.shelfView.rawValue, at: IndexPath(item: indexForNewDecoration, section: 0))!
        decoratorView.frame = rect
        cachedDecorationView.append(decoratorView)
        return decoratorView
    }
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return collectionView?.bounds.size != newBounds.size
    }

    override func invalidateLayout() {
        super.invalidateLayout()
        //delete all cached decoration views before new layout process
        cachedDecorationView.removeAll()
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45577420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档