ios - Swift collectionView 添加架子装饰器

标签 ios swift uicollectionview uicollectionviewlayout

我一直在布局中以类似架子的模式实现装饰 View 。首先,我想在滚动时生成新的decorationView元素,并且不将它们添加到缓存中。但是当我尝试使用最后一个单元格的indexPath 生成decorationView 时,在删除项目应用程序时立即崩溃。然后,我尝试为装饰 View 实现一些缓存,然后在 layoutAttributesForElements(in rect: CGRect) 中检查它们,但是应用程序开始做出奇怪的想法:如果我尝试删除其中一个项目,一开始没有在屏幕上显示,应用程序立即崩溃。但是,如果我首先删除这些元素中的几个元素,然后开始从末尾删除元素,则一切都会顺利。 如何更好地将这些装饰器添加到我的布局中?我现在的代码如下:

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

}

最佳答案

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

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()
    }

关于ios - Swift collectionView 添加架子装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577420/

相关文章:

ios - 如何在 collectionView 单元格中设置 NSLayoutConstraint 常量?

iphone - 使用ASIHTTPRequest发送的HTTPS请求返回NULL响应

objective-c - Objective-C 中的类属性列表

javascript - jQuery - 允许在移动设备上自动显示键盘

ios - 无法使用 'ios:' .concat(data) 在 iOS 的 Web View 中从服务器调用数据

ios - Swift 通用数组到特定类型

ios - 包含 UIImageView 导出的 UICollectionViewCell 子类不显示图像

ios - 如何修复 "Dictionary literal contains duplicate keys "?

ios - 如何在 Swift 中以编程方式为 UILabel 提供动态高度?

ios - 在 collectionView cellForItemAtIndexPath 中创建形状 - Objective C