简体   繁体   English

如何在 iOS Swift 4 中为 UITableView 的部分设置阴影?

[英]How to set drop shadow around a section for UITableView in iOS Swift 4?

I want to set shadow around a every section(group of cells) in iOS Swift.我想在 iOS Swift 中的每个部分(单元格组)周围设置阴影。 Like this:像这样:
在此处输入图片说明

Easy to achieve this with UICollectionView ,使用UICollectionView轻松实现这UICollectionView

by subclass UICollectionViewFlowLayout with DecorationView通过子类UICollectionViewFlowLayoutDecorationView

turn UITableViewCell into UICollectionViewCell ,UITableViewCell变成UICollectionViewCell

use decoration view to achieve the drop shadow of section part使用装饰视图实现剖面部分的阴影


class DecorationFlow: UICollectionViewFlowLayout {
    
    private var cache = [IndexPath: UICollectionViewLayoutAttributes]()
    
    override func prepare() {
        super.prepare()
        register(HistoDecorationV.self, forDecorationViewOfKind: HistoDecorationV.id)
        let originX: CGFloat = 15
        let widH: CGFloat = UI.std.width - originX * 2
        guard let collect = collectionView else{
            return
        }
        let sections = collect.numberOfSections
        var originY: CGFloat = 0
        for sect in 0..<sections{
            let sectionFirst = IndexPath(item: 0, section: sect)
            let attributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: HistoDecorationV.id, with: sectionFirst)
            let itemCount = collect.numberOfItems(inSection: sect)
            originY = originY + 80
            let h: CGFloat = 50 * CGFloat(itemCount)
            attributes.frame = CGRect(x: originX, y: originY, width: widH, height: h)
            originY = originY + h + 15
            cache[sectionFirst] = attributes
        }
    }
    
    
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    
    private func prepareCache() {
      cache.removeAll(keepingCapacity: true)
      cache = [IndexPath: UICollectionViewLayoutAttributes]()
    }
    
    
    override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        return cache[indexPath]
        
    }

    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var array = super.layoutAttributesForElements(in: rect)
        guard let collection = collectionView, collection.numberOfSections > 0 else{
            return array
        }
        var z = 0
        array?.forEach({
            $0.zIndex = z + 100
            z += 1
        })
        z = 0
        for (_, attributes) in cache {
            
            if attributes.frame.intersects(rect){
                attributes.zIndex = z + 10
                array?.append(attributes)
            }
            z += 1
        }
        return array
    }
}

more code in github github 中的更多代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM