| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- //
- // MultiRowColumnTable.m
- // IBOSS
- //
- // Created by apple on 16/10/19.
- // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved.
- //
- #import "MultiRowColumnTable.h"
- @implementation DataGridScrollView
- @synthesize dataGridComponent;
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *t = [touches anyObject];
- if([t tapCount] == 1){
- MultiRowColumnTable *d = (MultiRowColumnTable*)dataGridComponent;
- //int idx = [t locationInView:self].y / d.cellHeight;
- int idx = [self indexInView:([t locationInView:self].y) Data:d];
- if (idx == -1) {
- return;
- }
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.65];
- for(int i=0;i<[d.dataSource.titles count];i++){
- UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * 1000 + i ];
- l.alpha = .5;
- }
- for(int i=0;i<[d.dataSource.titles count];i++){
- UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * 1000 + i ];
- l.alpha = 1.0;
- }
- [UIView commitAnimations];
-
- // 点击事件
- if (d.clickDelegate && [d.clickDelegate respondsToSelector:@selector(ItemClick:)]) {
- [d.clickDelegate ItemClick:idx];
- }
- }
- }
- /**
- 取到当前行的index
-
- @param y <#y description#>
- @param d <#d description#>
-
- @return index
- */
- - (int)indexInView:(float)y Data:(MultiRowColumnTable *)d{
- int index = -1;
- float height = 0;
- for(int i=0;i<[d.dataSource.columnHeight count];i++){
- height += [d.dataSource.columnHeight[i] floatValue];
- if ( height>=y) {
- index = i;
- break;
- }
-
- }
- return index;
- }
- @end
- @implementation DataGridComponentDataSource
- @synthesize titles,data,columnWidth,columnHeight;
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.data = [[NSMutableArray alloc]init];
- self.columnHeight = [[NSMutableArray alloc]init];
- }
- return self;
- }
- @end
- //声明私有方法
- @interface MultiRowColumnTable()
- /**
- * 初始化各子视图
- */
- - (void)layoutSubView:(CGRect)aRect;
- @end
- @implementation MultiRowColumnTable
- @synthesize dataSource,cellHeight,vRight,vLeft,clickDelegate;
- - (id)initWithFrame:(CGRect)aRect data:(DataGridComponentDataSource*)aDataSource{
- self = [super initWithFrame:aRect];
- if(self != nil){
-
- self.clipsToBounds = YES;
- //self.backgroundColor = [UIColor whiteColor];
- self.dataSource = aDataSource;
- if (self.fontTitleSize == 0) {
- self.fontTitleSize = 15.0f;
- }
-
- if (self.fontContentSize == 0) {
- self.fontContentSize = 14.0f;
- }
- if (self.titleBackgroudColor == nil) {
- self.titleBackgroudColor = [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1];//[UIColor grayColor];
- }
-
- //初始显示视图及Cell的长宽高
- contentWidth = .0;
- cellHeight = 45.0;
- cellWidth = [[dataSource.columnWidth objectAtIndex:0] intValue];
- for(int i=1;i<[dataSource.columnWidth count];i++)
- contentWidth += [[dataSource.columnWidth objectAtIndex:i] intValue];
- contentHeight = [dataSource.data count] * cellHeight;
- contentWidth = contentWidth + [[dataSource.columnWidth objectAtIndex:0] intValue] < aRect.size.width
- ? aRect.size.width : contentWidth;
-
- //初始化各视图
- [self layoutSubView:aRect];
-
- // //填冲数据
- // [self fillData];
-
- }
- return self;
- }
- //初始化各视图
- - (void)layoutSubView:(CGRect)aRect{
- vLeftContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width, contentHeight)];
- vRightContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
-
- vLeftContent.opaque = YES;
- vRightContent.opaque = YES;
-
-
- //初始化各视图
- vTopLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
- vTopRight = [[UIView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, cellHeight)];
-
- vLeft = [[DataGridScrollView alloc] initWithFrame:CGRectMake(0, cellHeight, aRect.size.width, aRect.size.height - cellHeight)];
- vRight = [[DataGridScrollView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, contentHeight)];
-
- vLeft.dataGridComponent = self;
- vRight.dataGridComponent = self;
-
- vLeft.opaque = YES;
- vRight.opaque = YES;
- vTopLeft.opaque = YES;
- vTopRight.opaque = YES;
-
- //设置ScrollView的显示内容
- vLeft.contentSize = CGSizeMake(aRect.size.width, contentHeight);
- vRight.contentSize = CGSizeMake(contentWidth,aRect.size.height - cellHeight);
-
- //设置ScrollView参数
- vRight.delegate = self;
- vLeft.delegate = self;
-
- //vTopRight.backgroundColor = [UIColor whiteColor];
- //vRight.backgroundColor = [UIColor whiteColor];
- vTopLeft.backgroundColor =self.titleBackgroudColor ;//[UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1];
-
- //添加各视图
- [vRight addSubview:vRightContent];
- [vLeft addSubview:vLeftContent];
- [vLeft addSubview:vRight];
- [self addSubview:vTopLeft];
- [self addSubview:vLeft];
-
- [vLeft bringSubviewToFront:vRight];
- [self addSubview:vTopRight];
- [self bringSubviewToFront:vTopRight];
- }
- // 填冲数据
- - (void)fillData{
-
- float columnOffset = 0.0;
-
- //填冲标题数据
- for(int column = 0;column < [dataSource.titles count];column++){
- float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
- UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, 0, columnWidth, cellHeight )];
- l.font = [UIFont systemFontOfSize:self.fontTitleSize];
- l.text = [dataSource.titles objectAtIndex:column];
- l.backgroundColor = self.titleBackgroudColor;
- // l.textColor = [UIColor whiteColor];
- //l.textAlignment = NSTextAlignmentCenter;
- if( 0 == column){
- [vTopLeft addSubview:l];
- }
- else{
- [vTopRight addSubview:l];
- columnOffset += columnWidth;
- }
- }
-
- dataSource.columnHeight = [[NSMutableArray alloc]init];
- contentHeight = 0;
-
- //填冲数据内容
- for(int i = 0;i<[dataSource.data count];i++){
-
- NSArray *rowData = [dataSource.data objectAtIndex:i];
- columnOffset = 0.0;
- cellMaxHeight = cellHeight;
- for(int column=0;column<[rowData count];column++){
- float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
- UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight , columnWidth, cellHeight -1 )];
- l.numberOfLines = 0;//多行显示,计算高度
- l.font = [UIFont systemFontOfSize:self.fontContentSize];
- // 计算高度
- NSString *labelContent = [NSString stringWithFormat:@"%@", [rowData objectAtIndex:column]];
- CGSize testSize = [labelContent boundingRectWithSize:CGSizeMake(l.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.fontContentSize]} context:nil].size;
- l.text = labelContent;
- l.textAlignment = NSTextAlignmentLeft;
- l.tag = i * 1000 + column ;
- l.frame = CGRectMake(columnOffset, contentHeight , columnWidth, testSize.height );
-
- if( 0 == column){
- //l.backgroundColor = [UIColor grayColor];
- [vLeftContent addSubview:l];
- }
- else{
- //l.backgroundColor = [UIColor darkGrayColor];
- [vRightContent addSubview:l];
- columnOffset += columnWidth;
- }
- }
- contentHeight += cellMaxHeight;
- [dataSource.columnHeight addObject:[NSString stringWithFormat:@"%f",cellMaxHeight]];
-
- }
-
- // 重新绘制位置
- vRightContent.frame = CGRectMake(vRightContent.frame.origin.x, vRightContent.frame.origin.y , vRightContent.frame.size.width, contentHeight );
- vLeftContent.frame = CGRectMake(vLeftContent.frame.origin.x, vLeftContent.frame.origin.y , vLeftContent.frame.size.width, contentHeight );
- vLeft.contentSize = CGSizeMake(vLeft.frame.size.width, contentHeight + cellMaxHeight);
- //vRight.contentSize = CGSizeMake(vRight.frame.size.width, contentHeight+ cellMaxHeight);
-
- }
- //---------------以下为右侧scrollview左右滑动事件处发方法-------------------------
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
- if (scrollView == vLeft) {
- //NSLog(@"vLeft");
- return;
- }
-
- if (scrollView == vRight) {
- vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
- vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
- vTopRight.clipsToBounds = YES;
- vRightContent.frame = CGRectMake(0, 0 ,
- vRight.contentSize.width , contentHeight);
- [self addSubview:vTopRight];
- vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height);
- [vLeft addSubview:scrollView];
- }
-
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
-
- if (scrollView == vRight) {
- // vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
- // vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
- // vTopRight.clipsToBounds = YES;
- // vRightContent.frame = CGRectMake(0, 0 ,
- // vRight.contentSize.width , contentHeight);
- // [self addSubview:vTopRight];
- // vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height);
- // [vLeft addSubview:scrollView];
- }
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
- if (scrollView == vLeft) {
- //NSLog(@"vLeft");
- return;
- }
- if (scrollView == vRight) {
- //NSLog(@"vRight");
- scrollView.frame = CGRectMake(cellWidth, 0, scrollView.frame.size.width, self.frame.size.height);
- vRightContent.frame = CGRectMake(0, cellHeight - vLeft.contentOffset.y ,
- vRight.contentSize.width , contentHeight);
-
- vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
- vTopRight.bounds = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
- [scrollView addSubview:vTopRight];
- [self addSubview:scrollView];
-
- }
-
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
- if (scrollView == vLeft) {
- //NSLog(@"vLeft");
- return;
- }
- if(!decelerate)
- [self scrollViewDidEndDecelerating:scrollView];
- }
- @end
|