MultiRowColumnTable.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // MultiRowColumnTable.m
  3. // IBOSS
  4. //
  5. // Created by apple on 16/10/19.
  6. // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved.
  7. //
  8. #import "MultiRowColumnTable.h"
  9. @implementation DataGridScrollView
  10. @synthesize dataGridComponent;
  11. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  12. UITouch *t = [touches anyObject];
  13. if([t tapCount] == 1){
  14. MultiRowColumnTable *d = (MultiRowColumnTable*)dataGridComponent;
  15. //int idx = [t locationInView:self].y / d.cellHeight;
  16. int idx = [self indexInView:([t locationInView:self].y) Data:d];
  17. if (idx == -1) {
  18. return;
  19. }
  20. [UIView beginAnimations:nil context:nil];
  21. [UIView setAnimationDuration:0.65];
  22. for(int i=0;i<[d.dataSource.titles count];i++){
  23. UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * 1000 + i ];
  24. l.alpha = .5;
  25. }
  26. for(int i=0;i<[d.dataSource.titles count];i++){
  27. UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * 1000 + i ];
  28. l.alpha = 1.0;
  29. }
  30. [UIView commitAnimations];
  31. // 点击事件
  32. if (d.clickDelegate && [d.clickDelegate respondsToSelector:@selector(ItemClick:)]) {
  33. [d.clickDelegate ItemClick:idx];
  34. }
  35. }
  36. }
  37. /**
  38. 取到当前行的index
  39. @param y <#y description#>
  40. @param d <#d description#>
  41. @return index
  42. */
  43. - (int)indexInView:(float)y Data:(MultiRowColumnTable *)d{
  44. int index = -1;
  45. float height = 0;
  46. for(int i=0;i<[d.dataSource.columnHeight count];i++){
  47. height += [d.dataSource.columnHeight[i] floatValue];
  48. if ( height>=y) {
  49. index = i;
  50. break;
  51. }
  52. }
  53. return index;
  54. }
  55. @end
  56. @implementation DataGridComponentDataSource
  57. @synthesize titles,data,columnWidth,columnHeight;
  58. - (instancetype)init
  59. {
  60. self = [super init];
  61. if (self) {
  62. self.data = [[NSMutableArray alloc]init];
  63. self.columnHeight = [[NSMutableArray alloc]init];
  64. }
  65. return self;
  66. }
  67. @end
  68. //声明私有方法
  69. @interface MultiRowColumnTable()
  70. /**
  71. * 初始化各子视图
  72. */
  73. - (void)layoutSubView:(CGRect)aRect;
  74. @end
  75. @implementation MultiRowColumnTable
  76. @synthesize dataSource,cellHeight,vRight,vLeft,clickDelegate;
  77. - (id)initWithFrame:(CGRect)aRect data:(DataGridComponentDataSource*)aDataSource{
  78. self = [super initWithFrame:aRect];
  79. if(self != nil){
  80. self.clipsToBounds = YES;
  81. //self.backgroundColor = [UIColor whiteColor];
  82. self.dataSource = aDataSource;
  83. if (self.fontTitleSize == 0) {
  84. self.fontTitleSize = 15.0f;
  85. }
  86. if (self.fontContentSize == 0) {
  87. self.fontContentSize = 14.0f;
  88. }
  89. if (self.titleBackgroudColor == nil) {
  90. self.titleBackgroudColor = [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1];//[UIColor grayColor];
  91. }
  92. //初始显示视图及Cell的长宽高
  93. contentWidth = .0;
  94. cellHeight = 45.0;
  95. cellWidth = [[dataSource.columnWidth objectAtIndex:0] intValue];
  96. for(int i=1;i<[dataSource.columnWidth count];i++)
  97. contentWidth += [[dataSource.columnWidth objectAtIndex:i] intValue];
  98. contentHeight = [dataSource.data count] * cellHeight;
  99. contentWidth = contentWidth + [[dataSource.columnWidth objectAtIndex:0] intValue] < aRect.size.width
  100. ? aRect.size.width : contentWidth;
  101. //初始化各视图
  102. [self layoutSubView:aRect];
  103. // //填冲数据
  104. // [self fillData];
  105. }
  106. return self;
  107. }
  108. //初始化各视图
  109. - (void)layoutSubView:(CGRect)aRect{
  110. vLeftContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width, contentHeight)];
  111. vRightContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
  112. vLeftContent.opaque = YES;
  113. vRightContent.opaque = YES;
  114. //初始化各视图
  115. vTopLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
  116. vTopRight = [[UIView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, cellHeight)];
  117. vLeft = [[DataGridScrollView alloc] initWithFrame:CGRectMake(0, cellHeight, aRect.size.width, aRect.size.height - cellHeight)];
  118. vRight = [[DataGridScrollView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, contentHeight)];
  119. vLeft.dataGridComponent = self;
  120. vRight.dataGridComponent = self;
  121. vLeft.opaque = YES;
  122. vRight.opaque = YES;
  123. vTopLeft.opaque = YES;
  124. vTopRight.opaque = YES;
  125. //设置ScrollView的显示内容
  126. vLeft.contentSize = CGSizeMake(aRect.size.width, contentHeight);
  127. vRight.contentSize = CGSizeMake(contentWidth,aRect.size.height - cellHeight);
  128. //设置ScrollView参数
  129. vRight.delegate = self;
  130. vLeft.delegate = self;
  131. //vTopRight.backgroundColor = [UIColor whiteColor];
  132. //vRight.backgroundColor = [UIColor whiteColor];
  133. vTopLeft.backgroundColor =self.titleBackgroudColor ;//[UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1];
  134. //添加各视图
  135. [vRight addSubview:vRightContent];
  136. [vLeft addSubview:vLeftContent];
  137. [vLeft addSubview:vRight];
  138. [self addSubview:vTopLeft];
  139. [self addSubview:vLeft];
  140. [vLeft bringSubviewToFront:vRight];
  141. [self addSubview:vTopRight];
  142. [self bringSubviewToFront:vTopRight];
  143. }
  144. // 填冲数据
  145. - (void)fillData{
  146. float columnOffset = 0.0;
  147. //填冲标题数据
  148. for(int column = 0;column < [dataSource.titles count];column++){
  149. float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
  150. UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, 0, columnWidth, cellHeight )];
  151. l.font = [UIFont systemFontOfSize:self.fontTitleSize];
  152. l.text = [dataSource.titles objectAtIndex:column];
  153. l.backgroundColor = self.titleBackgroudColor;
  154. // l.textColor = [UIColor whiteColor];
  155. //l.textAlignment = NSTextAlignmentCenter;
  156. if( 0 == column){
  157. [vTopLeft addSubview:l];
  158. }
  159. else{
  160. [vTopRight addSubview:l];
  161. columnOffset += columnWidth;
  162. }
  163. }
  164. dataSource.columnHeight = [[NSMutableArray alloc]init];
  165. contentHeight = 0;
  166. //填冲数据内容
  167. for(int i = 0;i<[dataSource.data count];i++){
  168. NSArray *rowData = [dataSource.data objectAtIndex:i];
  169. columnOffset = 0.0;
  170. cellMaxHeight = cellHeight;
  171. for(int column=0;column<[rowData count];column++){
  172. float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
  173. UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight , columnWidth, cellHeight -1 )];
  174. l.numberOfLines = 0;//多行显示,计算高度
  175. l.font = [UIFont systemFontOfSize:self.fontContentSize];
  176. // 计算高度
  177. NSString *labelContent = [NSString stringWithFormat:@"%@", [rowData objectAtIndex:column]];
  178. CGSize testSize = [labelContent boundingRectWithSize:CGSizeMake(l.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.fontContentSize]} context:nil].size;
  179. l.text = labelContent;
  180. l.textAlignment = NSTextAlignmentLeft;
  181. l.tag = i * 1000 + column ;
  182. l.frame = CGRectMake(columnOffset, contentHeight , columnWidth, testSize.height );
  183. if( 0 == column){
  184. //l.backgroundColor = [UIColor grayColor];
  185. [vLeftContent addSubview:l];
  186. }
  187. else{
  188. //l.backgroundColor = [UIColor darkGrayColor];
  189. [vRightContent addSubview:l];
  190. columnOffset += columnWidth;
  191. }
  192. }
  193. contentHeight += cellMaxHeight;
  194. [dataSource.columnHeight addObject:[NSString stringWithFormat:@"%f",cellMaxHeight]];
  195. }
  196. // 重新绘制位置
  197. vRightContent.frame = CGRectMake(vRightContent.frame.origin.x, vRightContent.frame.origin.y , vRightContent.frame.size.width, contentHeight );
  198. vLeftContent.frame = CGRectMake(vLeftContent.frame.origin.x, vLeftContent.frame.origin.y , vLeftContent.frame.size.width, contentHeight );
  199. vLeft.contentSize = CGSizeMake(vLeft.frame.size.width, contentHeight + cellMaxHeight);
  200. //vRight.contentSize = CGSizeMake(vRight.frame.size.width, contentHeight+ cellMaxHeight);
  201. }
  202. //---------------以下为右侧scrollview左右滑动事件处发方法-------------------------
  203. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  204. if (scrollView == vLeft) {
  205. //NSLog(@"vLeft");
  206. return;
  207. }
  208. if (scrollView == vRight) {
  209. vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
  210. vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
  211. vTopRight.clipsToBounds = YES;
  212. vRightContent.frame = CGRectMake(0, 0 ,
  213. vRight.contentSize.width , contentHeight);
  214. [self addSubview:vTopRight];
  215. vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height);
  216. [vLeft addSubview:scrollView];
  217. }
  218. }
  219. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  220. if (scrollView == vRight) {
  221. // vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
  222. // vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
  223. // vTopRight.clipsToBounds = YES;
  224. // vRightContent.frame = CGRectMake(0, 0 ,
  225. // vRight.contentSize.width , contentHeight);
  226. // [self addSubview:vTopRight];
  227. // vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height);
  228. // [vLeft addSubview:scrollView];
  229. }
  230. }
  231. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  232. if (scrollView == vLeft) {
  233. //NSLog(@"vLeft");
  234. return;
  235. }
  236. if (scrollView == vRight) {
  237. //NSLog(@"vRight");
  238. scrollView.frame = CGRectMake(cellWidth, 0, scrollView.frame.size.width, self.frame.size.height);
  239. vRightContent.frame = CGRectMake(0, cellHeight - vLeft.contentOffset.y ,
  240. vRight.contentSize.width , contentHeight);
  241. vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
  242. vTopRight.bounds = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
  243. [scrollView addSubview:vTopRight];
  244. [self addSubview:scrollView];
  245. }
  246. }
  247. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  248. if (scrollView == vLeft) {
  249. //NSLog(@"vLeft");
  250. return;
  251. }
  252. if(!decelerate)
  253. [self scrollViewDidEndDecelerating:scrollView];
  254. }
  255. @end