SheetView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // SheetView.m
  3. // SheetViewDemo
  4. //
  5. // Created by Mengmin Duan on 2017/7/19.
  6. // Copyright © 2017年 Mengmin Duan. All rights reserved.
  7. //
  8. #import "SheetView.h"
  9. #import "ContentViewCell.h"
  10. static NSString *leftViewCellId = @"left.tableview.cell";
  11. static NSString *topViewCellId = @"top.collectionview.cell";
  12. static NSString *contentViewCellId = @"content.tableview.cell";
  13. @interface SheetLeftView : UITableView
  14. @end
  15. @implementation SheetLeftView
  16. @end
  17. @interface SheetTopView : UICollectionView
  18. @end
  19. @implementation SheetTopView
  20. @end
  21. @interface SheetContentView : UITableView
  22. @end
  23. @implementation SheetContentView
  24. @end
  25. @interface SheetView () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
  26. @property (nonatomic, strong) SheetLeftView *leftView;
  27. @property (nonatomic, strong) SheetTopView *topView;
  28. @property (nonatomic, strong) SheetContentView *contentView;
  29. @property (nonatomic, strong) UILabel *sheetHeadLabel;
  30. @end
  31. @implementation SheetView
  32. - (instancetype)initWithFrame:(CGRect)frame
  33. {
  34. if (self = [super initWithFrame:frame]) {
  35. //self.layer.borderColor = [[UIColor colorWithRed:0xe5 / 255.0 green:0xe5 / 255.0 blue:0xe5 / 255.0 alpha:1.0] CGColor];
  36. // self.layer.cornerRadius = 1.0f;
  37. //self.layer.borderWidth = 1.0f;
  38. self.leftView = [[SheetLeftView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  39. self.leftView.dataSource = self;
  40. self.leftView.delegate = self;
  41. self.leftView.showsVerticalScrollIndicator = NO;
  42. //self.leftView.backgroundColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1];
  43. // self.leftView.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  44. // self.leftView.layer.borderWidth = 1.0f;
  45. self.leftView.separatorStyle = UITableViewCellSeparatorStyleNone;
  46. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  47. layout.minimumLineSpacing = 1.0;
  48. layout.minimumInteritemSpacing = 0;
  49. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  50. self.topView = [[SheetTopView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  51. self.topView.dataSource = self;
  52. self.topView.delegate = self;
  53. self.topView.showsHorizontalScrollIndicator = NO;
  54. self.topView.bounces = NO;
  55. [self.topView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:topViewCellId];
  56. //self.topView.backgroundColor = [UIColor whiteColor];
  57. //self.topView.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  58. //self.topView.layer.borderWidth = 1.0f;
  59. self.contentView = [[SheetContentView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  60. self.contentView.dataSource = self;
  61. self.contentView.delegate = self;
  62. self.contentView.showsVerticalScrollIndicator = NO;
  63. self.contentView.bounces = NO;
  64. self.contentView.separatorStyle = UITableViewCellSeparatorStyleNone;
  65. self.contentView.backgroundColor = [UIColor whiteColor];
  66. [self addSubview:self.leftView];
  67. [self addSubview:self.topView];
  68. [self addSubview:self.contentView];
  69. }
  70. return self;
  71. }
  72. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  73. {
  74. if ([scrollView isKindOfClass:[SheetLeftView class]]) {
  75. self.contentView.contentOffset = self.leftView.contentOffset;
  76. }
  77. if ([scrollView isKindOfClass:[SheetContentView class]]) {
  78. self.leftView.contentOffset = self.contentView.contentOffset;
  79. }
  80. if ([scrollView isKindOfClass:[UICollectionView class]]) {
  81. for (ContentViewCell *cell in self.contentView.visibleCells) {
  82. cell.cellCollectionView.contentOffset = scrollView.contentOffset;
  83. }
  84. }
  85. }
  86. - (void)layoutSubviews
  87. {
  88. [super layoutSubviews];
  89. CGFloat sheetViewWidth = self.frame.size.width;
  90. CGFloat sheetViewHeight = self.frame.size.height;
  91. self.leftView.frame = CGRectMake(0, 0, self.titleColWidth, sheetViewHeight - self.titleRowHeight);
  92. self.topView.frame = CGRectMake(self.titleColWidth, sheetViewHeight - self.titleRowHeight, sheetViewWidth - self.titleColWidth, self.titleRowHeight);
  93. self.contentView.frame = CGRectMake(self.titleColWidth, 0, sheetViewWidth - self.titleColWidth, sheetViewHeight - self.titleRowHeight);
  94. UIScrollView *bgView = [[UIScrollView alloc]initWithFrame:self.topView.frame];
  95. // self.topView.backgroundColor = [UIColor blueColor];
  96. bgView.backgroundColor = [UIColor whiteColor];
  97. bgView.contentSize = CGSizeMake(self.topView.frame.size.width * 7, 0) ;
  98. UIView *bgsubView = [[UIView alloc]initWithFrame:CGRectMake(0, self.topView.frame.size.height/2 - 15, self.topView.frame.size.width * 7 , 30)];
  99. // gradient
  100. // CAGradientLayer *gl = [CAGradientLayer layer];
  101. // gl.frame = bgView.frame;
  102. // gl.startPoint = CGPointMake(1, 1);
  103. // gl.endPoint = CGPointMake(0, 0);
  104. // gl.colors = @[(__bridge id)[UIColor colorWithRed:254/255.0 green:132/255.0 blue:19/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:170/255.0 blue:0/255.0 alpha:1.0].CGColor];
  105. // gl.locations = @[@(0), @(1.0f)];
  106. // [bgsubView.layer addSublayer:gl];
  107. bgsubView.backgroundColor = [UIColor orangeColor];
  108. bgsubView.layer.cornerRadius = 15;
  109. [bgView addSubview:bgsubView];
  110. self.topView.backgroundView = bgView;
  111. if (self.sheetHeadLabel) {
  112. [self.sheetHeadLabel removeFromSuperview];
  113. }
  114. self.sheetHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, sheetViewHeight - self.titleRowHeight, self.titleColWidth, self.titleRowHeight)];
  115. self.sheetHeadLabel.text = self.sheetHead;
  116. self.sheetHeadLabel.textColor = [UIColor greenColor];
  117. self.sheetHeadLabel.textAlignment = NSTextAlignmentCenter;
  118. self.sheetHeadLabel.backgroundColor = [UIColor whiteColor];
  119. // self.sheetHeadLabel.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  120. // self.sheetHeadLabel.layer.borderWidth = 1.0;
  121. [self addSubview:self.sheetHeadLabel];
  122. // NSLog(@"总宽度 ---- %f --- %f " ,self.leftView.contentSize.width,self.contentView.contentSize.width);
  123. }
  124. - (void)reloadData
  125. {
  126. // self.sheetHeadLabel.frame = CGRectMake(0, 0, self.titleColWidth, self.titleRowHeight);
  127. [self.leftView reloadData];
  128. [self.topView reloadData];
  129. [self.contentView reloadData];
  130. }
  131. # pragma mark -- UITableViewDelegate && UITableViewDataSource
  132. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  133. {
  134. return [self.delegate sheetView:self heightForRowAtIndexPath:indexPath];
  135. }
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. return [self.dataSource sheetView:self numberOfRowsInSection:section];
  139. }
  140. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. if ([tableView isKindOfClass:[SheetLeftView class]]) {
  143. UITableViewCell *leftCell = [tableView dequeueReusableCellWithIdentifier:leftViewCellId];
  144. if (leftCell == nil)
  145. {
  146. leftCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:leftViewCellId];
  147. leftCell.selectionStyle = UITableViewCellSelectionStyleNone;
  148. }
  149. for (UIView *view in leftCell.contentView.subviews) {
  150. [view removeFromSuperview];
  151. }
  152. leftCell.backgroundColor = [UIColor whiteColor];
  153. // leftCell.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  154. //leftCell.layer.borderWidth = 1;
  155. //
  156. // CGFloat width = self.titleColWidth;
  157. // CGFloat height = [self.delegate sheetView:self heightForRowAtIndexPath:indexPath];
  158. CGRect rect = CGRectMake(15, 10, 120, 30);
  159. UILabel *label = [[UILabel alloc] initWithFrame:rect];
  160. //label.backgroundColor = [UIColor redColor];
  161. label.numberOfLines = 0;
  162. label.text = [self.dataSource sheetView:self cellForLeftColAtIndexPath:indexPath];
  163. label.textColor = [UIColor colorWithRed:34/255 green:34/255 blue:34/255 alpha:1];
  164. label.font = [UIFont systemFontOfSize:11];
  165. label.textAlignment = NSTextAlignmentCenter;
  166. label.layer.masksToBounds = YES;
  167. label.layer.cornerRadius = 15 ;
  168. label.layer.borderColor = [UIColor colorWithRed:227/255 green:227/255 blue:227/255 alpha:1].CGColor;
  169. label.layer.borderWidth = 0.5f;
  170. [leftCell.contentView addSubview:label];
  171. return leftCell;
  172. }
  173. ContentViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:contentViewCellId];
  174. if (contentCell == nil)
  175. {
  176. contentCell = [[ContentViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentViewCellId];
  177. contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
  178. }
  179. contentCell.cellForItemBlock = ^NSString *(NSIndexPath *indexPathInner) {
  180. return [self.dataSource sheetView:self cellForContentItemAtIndexRow:indexPath indexCol:indexPathInner];
  181. };
  182. contentCell.numberOfItemsBlock = ^NSInteger(NSInteger section) {
  183. return [self.dataSource sheetView:self numberOfColsInSection:section];
  184. };
  185. contentCell.sizeForItemBlock = ^CGSize(UICollectionViewLayout * collectionViewLayout, NSIndexPath *indexPathInner) {
  186. CGFloat width = [self.delegate sheetView:self widthForColAtIndexPath:indexPathInner];
  187. CGFloat height = [self.delegate sheetView:self heightForRowAtIndexPath:indexPath];
  188. return CGSizeMake(width, height);
  189. };
  190. contentCell.contentViewCellDidScrollBlock = ^(UIScrollView *scroll) {
  191. for (ContentViewCell *cell in self.contentView.visibleCells) {
  192. cell.cellCollectionView.contentOffset = scroll.contentOffset;
  193. }
  194. self.topView.contentOffset = scroll.contentOffset;
  195. };
  196. if ([self.dataSource respondsToSelector:@selector(sheetView:cellWithColorAtIndexRow:)]) {
  197. contentCell.cellWithColorBlock = ^BOOL(NSIndexPath *indexPathInner) {
  198. return [self.dataSource sheetView:self cellWithColorAtIndexRow:indexPath];
  199. };
  200. }
  201. contentCell.cellDidSelectBlock = ^(NSIndexPath *indexPathInner) {
  202. if ([self.delegate respondsToSelector:@selector(sheetView:didSelectItemAtIndexRow:indexCol:)]) {
  203. [self.delegate sheetView:self didSelectItemAtIndexRow:indexPath indexCol:indexPathInner];
  204. }
  205. };
  206. contentCell.backgroundColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1];
  207. contentCell.cellCollectionView.frame = CGRectMake(0, 0, self.frame.size.width - self.titleColWidth, [self.delegate sheetView:self heightForRowAtIndexPath:indexPath]);
  208. [contentCell.cellCollectionView reloadData];
  209. return contentCell;
  210. }
  211. //- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  212. //{
  213. // if ([tableView isKindOfClass:[SheetLeftView class]]) {
  214. // return;
  215. // }
  216. // ContentViewCell *willDisplayCell = (ContentViewCell *)cell;
  217. // ContentViewCell *didDisplayCell = (ContentViewCell *)[tableView.visibleCells firstObject];
  218. // if (willDisplayCell.cellCollectionView && didDisplayCell.cellCollectionView && willDisplayCell.cellCollectionView.contentOffset.x != didDisplayCell.cellCollectionView.contentOffset.x) {
  219. // willDisplayCell.cellCollectionView.contentOffset = didDisplayCell.cellCollectionView.contentOffset;
  220. // }
  221. //}
  222. -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  223. if ([tableView isKindOfClass:[SheetLeftView class]]) {
  224. return;
  225. }
  226. ContentViewCell *willDisplayCell = (ContentViewCell *)cell;
  227. ContentViewCell *didDisplayCell = (ContentViewCell *)[tableView.visibleCells firstObject];
  228. if (willDisplayCell.cellCollectionView && didDisplayCell.cellCollectionView && willDisplayCell.cellCollectionView.contentOffset.x != didDisplayCell.cellCollectionView.contentOffset.x) {
  229. willDisplayCell.cellCollectionView.contentOffset = didDisplayCell.cellCollectionView.contentOffset;
  230. }
  231. }
  232. # pragma mark -- UICollectionViewDelegate && UICollectionViewDataSource
  233. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  234. {
  235. return 1;
  236. }
  237. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  238. {
  239. return [self.dataSource sheetView:self numberOfColsInSection:section];
  240. }
  241. - (CGSize)collectionView:(UICollectionView *)uiCollectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  242. {
  243. CGFloat width = [self.delegate sheetView:self widthForColAtIndexPath:indexPath];
  244. CGFloat height = self.titleRowHeight;
  245. return CGSizeMake(width, height);
  246. }
  247. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  248. {
  249. UICollectionViewCell *topCell = [collectionView dequeueReusableCellWithReuseIdentifier:topViewCellId forIndexPath:indexPath];
  250. if (topCell) {
  251. for (UIView *view in topCell.contentView.subviews) {
  252. [view removeFromSuperview];
  253. }
  254. // topCell.backgroundColor = [UIColor colorWithRed:(0xf0 / 255.0)green:(0xf0 / 255.0)blue:(0xf0 / 255.0)alpha:1];
  255. //topCell.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  256. //topCell.layer.borderWidth = 1;
  257. CGFloat width = [self.delegate sheetView:self widthForColAtIndexPath:indexPath];
  258. CGFloat height = self.titleRowHeight;
  259. CGRect rect = CGRectMake(0, 0, width, height);
  260. UILabel *label = [[UILabel alloc] initWithFrame:rect];
  261. label.text = [self.dataSource sheetView:self cellForTopRowAtIndexPath:indexPath];
  262. label.textColor = [UIColor whiteColor];
  263. label.font = [UIFont systemFontOfSize:11];
  264. label.textAlignment = NSTextAlignmentCenter;
  265. [topCell.contentView addSubview:label];
  266. }
  267. return topCell;
  268. }
  269. @end