代码之家  ›  专栏  ›  技术社区  ›  Majster

UITableView重叠单元格(力)

  •  0
  • Majster  · 技术社区  · 9 年前

    我希望实现表视图单元格重叠,因为我希望实现此效果:

    stacking

    所以基本上,细胞会一个堆叠在另一个上。我的目标是iOS7+(目前使用iOS8进行测试)。

    我目前正在做 CAGradientLayer 它被定位在 layoutSubviews 。我的定位是:

    CGRect gradientLayerFrame = self.contentView.bounds;
    gradientLayerFrame.origin.y = -6.0f;
    gradientLayerFrame.size.height += 6.0f;
    self.gradientLayer.frame = gradientLayerFrame;
    

    我也做了

    self.clipsToBounds = NO;
    self.contentView.clipsToBounds = NO;
    self.contentView.superview.clipsToBounds = NO;
    

    在里面 init 这样什么都不会夹。现在,单元格在第一次渲染时会被剪裁,但当我向下滚动表视图,然后再向上滚动,以便再次显示单元格时,它们不会被剪裁。

    我已经试过设置了 backgroundColor 清除 willDisplayCell 没有运气。还尝试递归遍历所有单元格子视图,并将clipping设置为NO,但再次执行相同的操作。还尝试了黑客攻击和强行设置 setNeedsDisplay 这样事情会重新呈现,但没有运气。

    在此问题上的任何帮助都将不胜感激。

    2 回复  |  直到 9 年前
        1
  •  2
  •   AlexZd    9 年前

    它不应该是TableView。

    尝试此库: TGLStackedViewController

        2
  •  2
  •   Nisha Yadav    7 年前

    前段时间我也遇到过同样的问题。

    对我有效的解决方案是使用集合视图而不是TableView。通过从UICollectionViewFlowLayout扩展自定义类,并在集合视图中使用该类。

    class OverlappedCustomFlowLayout: UICollectionViewFlowLayout {
        override func prepare() {
            super.prepare()
    
            // This allows us to make intersection and overlapping
            // A negative number implies overlapping whereas positive implies space between the adjacent edges of two cells.
            minimumLineSpacing = -100
        }
    
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
            let layoutAttributes = super.layoutAttributesForElements(in: rect)
            for currentLayoutAttributes: UICollectionViewLayoutAttributes in layoutAttributes! {
    
                // zIndex - Specifies the item’s position on the z-axis. 
                // Unlike a layer's zPosition, changing zIndex allows us to change not only layer position, 
                // but tapping/UI interaction logic too as it moves the whole item.
                currentLayoutAttributes.zIndex = currentLayoutAttributes.indexPath.row + 1
            }
        }
    
        return layoutAttributes
    }
    

    注:根据苹果的开发者文档 zIndex

    此属性用于确定项目的前后顺序 在布局期间。索引值较高的项目显示在项目的顶部 具有较低的值。具有相同值的项具有未确定的 顺序此属性的默认值为0。

    希望它有帮助!:)