#import
#define kRED @"1"
#define kBLACK @"0"
#define kGREEN @"2"
/**
* DataGrid所用数据源对象
*/
@interface DataGridComponentDataSource : NSObject
{
/**
* 标题列表
*/
NSMutableArray *titles;
/**
* 数据体,其中包函其它列表(NSArray)
*/
NSMutableArray *data;
/**
* 列宽
*/
NSMutableArray *columnWidth;
}
@property(retain) NSMutableArray *titles;
@property(retain) NSMutableArray *data;
@property(retain) NSMutableArray *columnWidth;
@end
@interface DataGridScrollView : UIScrollView
{
id dataGridComponent;
}
@property(assign)id dataGridComponent;
@end
/**
* 数据列表组件,支持上下与左右滑动
*/
@interface DataGridComponent : UIView<UIScrollViewDelegate> {
//左下列视图
DataGridScrollView *vLeft;
//右下列视图
DataGridScrollView *vRight;
//右下列表内容
UIView *vRightContent;
//左下列表内容
UIView *vLeftContent;
//右上标题
UIView *vTopRight;
//左上标题
UIView *vTopLeft;
//列表数据源
DataGridComponentDataSource *dataSource;
//内容总高度
float contentHeight ;
//内容总宽度
float contentWidth;
//单元格默认高度
float cellHeight;
//单元格默认宽度
float cellWidth;
}
@property(readonly) DataGridScrollView *vRight;
@property(readonly) DataGridScrollView *vLeft;
@property(readonly) float cellHeight;
@property(retain) DataGridComponentDataSource *dataSource;
/**
* 用指定显示区域与数据源初始化对象
*/
- (id)initWithFrame:(CGRect)aRect data:(DataGridComponentDataSource*)aDataSource;
@end
.m
#import "DataGridComponent.h"
@implementation DataGridScrollView
@synthesize dataGridComponent;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
if([t tapCount] == 1){
DataGridComponent *d = (DataGridComponent*)dataGridComponent;
int idx = [t locationInView:self].y / d.cellHeight;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.65];
for(int i=0;i<[d.dataSource.titles count];i++){
UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
l.alpha = .5;
}
for(int i=0;i<[d.dataSource.titles count];i++){
UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
l.alpha = 1.0;
}
[UIViewcommitAnimations];
}
}
@end
@implementation DataGridComponentDataSource
@synthesize titles,data,columnWidth;
- (void) dealloc
{
[data release];
[titles release];
[columnWidthrelease];
[superdealloc];
}
@end
//声明私有方法
@interface DataGridComponent(Private)
/**
* 初始化各子视图
*/
-(void)layoutSubView:(CGRect)aRect;
/**
* 用数据项填冲数据
*/
-(void)fillData;
@end
@implementation DataGridComponent
@synthesize dataSource,cellHeight,vRight,vLeft;
- (id)initWithFrame:(CGRect)aRect data:(DataGridComponentDataSource*)aDataSource{
self = [super initWithFrame:aRect];
if(self != nil){
self.clipsToBounds = YES;
//self.backgroundColor = [UIColor blackColor];
self.backgroundColor = [UIColor colorWithRed:71.0/255 green:71.0/255 blue:71.0/255 alpha:1];
self.dataSource = aDataSource;
//初始显示视图及Cell的长宽高
contentWidth = .0;
cellHeight = 40.0;
cellWidth = [[dataSource.columnWidthobjectAtIndex:0] intValue];
for(int i=1;i<[dataSource.columnWidthcount];i++)
contentWidth += [[dataSource.columnWidthobjectAtIndex:i] intValue];
contentHeight = [dataSource.datacount] * cellHeight;
contentWidth = contentWidth + [[dataSource.columnWidthobjectAtIndex:0] intValue] < aRect.size.width
? aRect.size.width : contentWidth;
//初始化各视图
[selflayoutSubView:aRect];
//填冲数据
[selffillData];
}
returnself;
}
-(void)layoutSubView:(CGRect)aRect{
vLeftContent = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, 60, contentHeight)];
vRightContent = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
vLeftContent.opaque = YES;
vRightContent.opaque = YES;
//初始化各视图
vTopLeft = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight+2)];
vLeft = [[DataGridScrollViewalloc] initWithFrame:CGRectMake(0, cellHeight+2, aRect.size.width, aRect.size.height - cellHeight-2)];
vRight = [[DataGridScrollViewalloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, contentHeight)];
vTopRight = [[UIViewalloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, cellHeight+2)];
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;
vTopRight.backgroundColor=[UIColorcolorWithRed:71.0/255green:71.0/255blue:71.0/255alpha:1];
vRight.backgroundColor=[UIColorcolorWithRed:71.0/255green:71.0/255blue:71.0/255alpha:1];
vTopLeft.backgroundColor=[UIColorcolorWithRed:71.0/255green:71.0/255blue:71.0/255alpha:1];
//vTopRight.backgroundColor = [UIColor blackColor];
//vRight.backgroundColor = [UIColor blackColor];
//vTopLeft.backgroundColor = [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1];
//添加各视图
[vRightaddSubview:vRightContent];
[vLeftaddSubview:vLeftContent];
[vLeftaddSubview:vRight];
[selfaddSubview:vTopLeft];
[selfaddSubview:vLeft];
[vLeftbringSubviewToFront:vRight];
[selfaddSubview:vTopRight];
[selfbringSubviewToFront:vTopRight];
}
-(void)fillData{
float columnOffset = 0.0;
int iColorRed=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 -1, cellHeight+2 )];
l.font = [UIFontsystemFontOfSize:16.0f];
l.text = [dataSource.titles objectAtIndex:column];
//l.backgroundColor = [UIColor colorWithRed:0.0/255 green:105.0/255 blue:186.0/255 alpha:1];
l.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed:@"bgtopbg"]];
l.textColor = [UIColorwhiteColor];
l.shadowColor = [UIColorblackColor];
l.shadowOffset = CGSizeMake(0, -0.5);
l.textAlignment = UITextAlignmentCenter;
if( 0 == column){
[vTopLeft addSubview:l];
}
else{
[vTopRight addSubview:l];
columnOffset += columnWidth;
}
[l release];
}
//填冲数据内容
for(int i = 0;i<[dataSource.data count];i++){
NSArray *rowData = [dataSource.data objectAtIndex:i];
columnOffset = 0.0;
for(int column=0;column<[rowData count];column++){
//第1个字段表示是否显示红色字体
if(column==0)
{
if([[rowData objectAtIndex:0] intValue] == 1)
{
iColorRed=1;
}
else if([[rowData objectAtIndex:0] intValue] == 2)
{
iColorRed=2;
}
else
iColorRed=0;
}
else
{
float columnWidth = [[dataSource.columnWidth objectAtIndex:column-1] floatValue];;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight , columnWidth-1, cellHeight -1 )];
l.font = [UIFont systemFontOfSize:14.0f];
l.text = [rowData objectAtIndex:column];
// if(column==1)
// {
// l.textAlignment = UITextAlignmentLeft;
// }
// else {
// l.textAlignment = UITextAlignmentCenter;
// }
l.textAlignment = UITextAlignmentCenter;
l.tag = i * cellHeight + column + 1000;
if(i % 2 == 0)
l.backgroundColor = [UIColor colorWithRed:59.0/255 green:59.0/255 blue:59.0/255 alpha:1];
else
l.backgroundColor = [UIColor colorWithRed:49.0/255 green:49.0/255 blue:49.0/255 alpha:1];
if (iColorRed==1)
{
l.textColor=[UIColor redColor];
}
if (iColorRed==2)
{
l.textColor=[UIColor colorWithRed:0.0/255 green:180.0/255 blue:90.0/255 alpha:1];
}
if (iColorRed==0)
{
l.textColor=[UIColor whiteColor];
}
if( 1 == column){
l.frame = CGRectMake(columnOffset, i * cellHeight , columnWidth -1 , cellHeight -1 );
[vLeftContent addSubview:l];
}
else if( 1 < column) {
[vRightContent addSubview:l];
columnOffset += columnWidth;
}
[l release];
}
}
}
}
//-------------------------------以下为事件处发方法----------------------------------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
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);
[selfaddSubview:vTopRight];
vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height);
[vLeft addSubview:scrollView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
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(!decelerate)
[selfscrollViewDidEndDecelerating:scrollView];
}
- (void) dealloc
{
[vLeftrelease];
[vRightrelease];
[vRightContentrelease];
[vLeftContentrelease];
[vTopLeftrelease];
[vTopRightrelease];
[superdealloc];
}
@end