项目介绍:
上次没有传demo 附件的github 地址贴出来。这次贴出来https://github.com/conquerLust/FromLikeExcel
类似excel跨行跨列的表格
采用UICollectionView实现,关键是自定义UICollectionViewLayoutAttributes 自定义了cell的大小来实现跨行跨列表格
关键代码如下:
for (int section = 0; section < [self.collectionView numberOfSections]; section ++) { NSMutableArray *sectionAttributes = [@[] mutableCopy]; for (NSUInteger row = 0; row < _allColumns; row++) { CGSize itemSize = [self.itemsSize[section][row] CGSizeValue]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:section]; UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attributes.frame = CGRectIntegral(CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height)); if (section == 0 && row < _lockColumn) { attributes.zIndex = 2015; } else if (section == 0 || row < _lockColumn) { attributes.zIndex = 2014; } if (section == 0) { CGRect frame = attributes.frame; frame.origin.y = self.collectionView.contentOffset.y; attributes.frame = frame; }
if (row < _lockColumn) { CGRect frame = attributes.frame; float offsetX = 0; if (index > 0) { for (int i = 0; i < row; i++) { offsetX += MinmumItemWidth; } }
frame.origin.x = self.collectionView.contentOffset.x + offsetX; attributes.frame = frame; }
[sectionAttributes addObject:attributes];
xOffset = xOffset + MinmumItemWidth; column ++;
if (column == _allColumns) { if (xOffset > contentWidth) { contentWidth = xOffset; }
// 重置基本变量 column = 0; xOffset = 0; yOffset += MinmumItemHeight; } } [self.itemAttributes addObject:sectionAttributes]; }
// 获取右下角最有一个item,确定collectionView的contentSize大小 UICollectionViewLayoutAttributes *attributes = [[self.itemAttributes lastObject] lastObject]; contentHeight = attributes.frame.origin.y + attributes.frame.size.height; _contentSize = CGSizeMake(contentWidth, contentHeight);