解决此问题的最佳方法是以某种方式告诉每个单元格是在一个部分的最后一行还是在一行中的最后一个单元格;然后单元格将关闭有问题的边框,部分标题将绘制顶部边框和底部边框,并且一切都将是笨拙的。不过,我不知道如何实现这一目标。
您所描述的或多或少是我在类似情况下所做的。我在我的单元格中添加了一个边框属性:
typedef NS_OPTIONS(NSInteger, TLGridBorder) {
TLGridBorderNone = 0,
TLGridBorderTop = 1 << 0,
TLGridBorderRight = 1 << 1,
TLGridBorderBottom = 1 << 2,
TLGridBorderLeft = 1 << 3,
TLGridBorderAll = TLGridBorderTop | TLGridBorderRight | TLGridBorderBottom | TLGridBorderLeft,
};
@interface TLGridCellView : UIView
@property (nonatomic) TLGridBorder border;
@end
然后我在视图控制器的单元格配置中设置边框:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TLGridCellView *cell = ...;
if (indexPath.item == self collectionView:collectionView numberOfItemsInSection:indexPath.section - 1) {
cell.border = TLGridBorderLeft;
} else {
cell.border = TLGridBorderLeft | TLGridBorderRight;
}
return cell;
}