简体   繁体   English

Swift:UICollectionViewCell的某些侧面带有边框

[英]Swift: border on UICollectionViewCell for some sides

For an app I am currently building I need a grid like UICollectionView with 3x3 UICollectionViewCell 's displayed. 对于当前正在构建的应用程序,我需要一个显示3x3 UICollectionViewCell的网格,如UICollectionView The UICollectionView will have the same width as the screen, and therefore I want a border only shown on the right and bottom side of the UICollectionViewCell . UICollectionView将具有与屏幕相同的宽度,因此我只希望在UICollectionViewCell的右侧和底部显示UICollectionViewCell It would have to look like this: 它必须看起来像这样:

    -----------------------
     cell1 | cell4 | cell7
    -----------------------
     cell2 | cell5 | cell8
    -----------------------
     cell3 | cell6 |
    -----------------------

I think it would work if I could somehow draw a line on the bottom of an invisible UICollectionView header, and the same on the UICollectionView footer (a line on the top). 我认为如果我能以某种方式在不可见的UICollectionView标头的底部绘制一条线,并在UICollectionView页脚的顶部UICollectionView一条线(在顶部的一条线),那将是UICollectionView An then for each UICollectionViewCell I could draw the missing lines. 然后,对于每个UICollectionViewCell我可以绘制缺失的行。

However I don't know how to do this, and could not find anything on the subject that worked (somewhat) the same. 但是我不知道该怎么做,并且在这个主题上(在某种程度上)没有发现任何东西。

You could put a view representing the line in your cell subclass. 您可以在单元格子类中放置代表该行的视图。 That is not the best performing solution, but sufficient in most cases, plus you do not need to write any code. 那不是最佳性能的解决方案,但是在大多数情况下足够了,而且您无需编写任何代码。 In your datasource method you could then set this line as hidden or not based on the index path. 然后,您可以在datasource方法中根据索引路径将此行设置为hidden或不hidden

Eg, if you do not want a line on the right if it is the third cell in a row, do something like 例如,如果您不想在一行中的第三行右边显示一行,请执行以下操作

cell.rightBorder.hidden = indexPath.item % 3 == 0

You can achieve this by subclassing the cell and overriding drawRect. 您可以通过对单元格进行子类化并覆盖drawRect来实现此目的。 There you can draw a bottom and right border. 您可以在此处绘制底部和右侧边框。

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

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