自己开发的Grid组件 针对IOS的

来源:互联网 发布:linux系统 编辑:程序博客网 时间:2024/05/22 14:56



2012-12-24此grid成功在iphone 设备上使用


实现功能的说明,可以对多个grid的联动,实现了无限翻页.

grid功能是,右标题的右边内容可以联动,内容区可以单独翻页.

非常好扩展



第一部门为头文件:目前没有太多时间解释代码的.希望对大家有用

//

//  NQGrid.h

//  IpadBlackDemo

//

//  Created by ch_soft on 11-11-17.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import<UIKit/UIKit.h>

#import"NQDataSource.h"

#import"MapColor.h"

#import"NQScorllPage.h"

#import"NQScorllPageChildView.h"

@classContentPage;



@interface RightContentView :UIView <UIScrollViewDelegate>

{

    UIScrollView * rightContentScrollView;

    NSMutableArray * rightCellArray;

    

}

@property (nonatomic,assign)NSInteger rowNumber;

@property (nonatomic,retain)NSMutableArray * consultRightTitle;


@property (nonatomic,assign)id rightContentDelegate;


-(void)initRightContent:(NSMutableArray *)consultRight;

//要填充的数据及对照的标题

-(void)fillRightContentData:(NSMutableArray *) data;


-(void)clearRightContentData;

@end


@interface LeftContentView :UIView 

{

    NSMutableArray * leftCellArray;

}

@property (nonatomic,assign)NSInteger rowNumber;

@property (nonatomic,retain)NSMutableArray * consultLeftTitle;

@property (nonatomic,assign)id leftContentDelegate;

-(void)initLeftContent:(NSMutableArray *) consultLeft;


-(void)fillLeftContentData:(NSMutableArray *) data;


-(void)clearLeftContentData;

@end






//中间内容视图

@interface GridContentView :NQScorllPageChildView

{

    LeftContentView * leftContentView;

    RightContentView * rightContentView;

    UIButton * hxView;

}

@property (nonatomic,assign)NSInteger fixTitle;

@property (nonatomic,assign)NSInteger rowNumber;

@property (nonatomic,assign)BOOL isPreFixData;//是否预加载fixTitle的数据

@property (nonatomic,assign)id contentDelegate;

@property (nonatomic,retain)LeftContentView * leftContentView;

@property (nonatomic,retain)RightContentView * rightContentView;


-(void)initContent:(NQDataSource *)dataSource;


-(void)fillContentData:(NSMutableArray *)data;


-(void)clearData;

@end








@interface ContentPage :UIView<NQScorllPageDelegate

{

    NQScorllPage * scorllPage;

   GridContentView * last;

   GridContentView * next;

    GridContentView * current;

    

}

@property (nonatomic ,retain)NQScorllPage * scorllPage;

@property (nonatomic ,assign)id contentPageDelegate;

@property (nonatomic ,assign)NSInteger fixTitle;//固定表头的个数

@property (nonatomic ,assign)NSInteger rowNumber;//行的个数

@property (nonatomic,assign)BOOL isPreFixData;//是否预加载fixTitle的数据


@property (nonatomic ,assign)NSInteger pageMax;//最大页数


-(void)initPage:(NQDataSource *)ds;

-(void)fillPageData:(NSMutableArray* )data;

@end


@interface NQGrid :UIView


//初始化属性

@property (nonatomic,assign)CGFloat titleHieght;//标题的高度

@property (nonatomic,assign)CGFloat contentHieght;//内容区的高度

@property (nonatomic,assign)NSInteger fixTitleNumber;//固定表头的个数

@property (nonatomic,assign)NSInteger rowNumber;//行的个数

@property (nonatomic,assign)BOOL isPreFixData;//是否预加载fixTitle的数据

@property (nonatomic,assign)NSInteger maxDataNumber;//最大的数据量

@property (nonatomic,assign)id delegate;



@property (nonatomic,retain)ContentPage * contentPage;


@property (nonatomic,retain)NQDataSource * dataSource;


- (id)initWithFrame:(CGRect)frame withTitleHeight:(NSInteger) _titleHieght;

-(void)initGrid:(NQDataSource *)ds;


-(void)fillData:(NSMutableArray *) data ;

@end



第二部分,源文件部分,注释写的少,请见谅

//

//  NQGrid.m

//  IpadBlackDemo

//

//  Created by ch_soft on 11-11-17.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import"NQGrid.h"

@interface Cell :UIButton

{

    NSMutableArray *lableArray;

}

@property (nonatomic,retain)NSMutableArray *lableArray;

-(id)initWithFrame:(CGRect)frame andTitles:(NSMutableArray *)titleArray;

@end

@implementation Cell

@synthesize lableArray;


-(id)initWithFrame:(CGRect)frame 

{

    self = [super initWithFrame:frame];

    if (self) {

        lableArray = [[NSMutableArray alloc] init];

        for(int i = 0;i < 17;i++)

        {

            UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(i*(924.0/8.0),0, (924.0/8.0)-13,66.0)];

            temp.backgroundColor = [UIColorclearColor];

            [selfaddSubview:temp];

            temp.textAlignment =UITextAlignmentRight;

            [lableArrayaddObject:temp];

            [temp release];

        }

        

    }

   returnself;

}



- (CGFloat) getWidthAt:(NSInteger)index In:(NSMutableArray * )array;

{

    if (index<[array count]) {

        CGFloat ww = [[[array objectAtIndex:index]objectForKey:@"width"]floatValue];

        if (ww==0) {

            ww = 114.5;

        }

        return ww;

    }else{

        return 0;

    }

}


-(id)initWithFrame:(CGRect)frame andTitles:(NSMutableArray *)titleArray;

{

    self = [super initWithFrame:frame];

    if (self) {

        CGFloat totalWidth=0;

        lableArray = [[NSMutableArray alloc] init];

        for(int i = 0;i <[titleArray count];i++)

        {

            CGFloat width=[self getWidthAt:i In:titleArray];

            UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(totalWidth,0, width,frame.size.height)];

            temp.backgroundColor = [UIColorclearColor];

            [selfaddSubview:temp];

            temp.textAlignment =UITextAlignmentRight;

            [lableArrayaddObject:temp];

            [temp release];

            

            UIView * bgview=[[UIView alloc] initWithFrame:CGRectMake(totalWidth,0,1, frame.size.height)];

            bgview.backgroundColor=[UIColorgrayColor];

            [selfaddSubview:bgview];

            [bgview release];

            

            totalWidth=totalWidth+width;

        }

        self.frame=CGRectMake(0, frame.origin.y, totalWidth, frame.size.height);

        

    }

   returnself;

}


- (UILabel *)getCellWithIndex:(NSInteger)index;

{

    if ([lableArray count]<=index) {

        NSInteger count = index - [lableArray count];

        for (NSInteger i=0; i<=count; i++) {

            UILabel *temp = [[UILabel alloc] initWithFrame:CGRectZero];

            [lableArrayaddObject:temp];

            [selfaddSubview:temp];

            [temp release];

        }

    }

    return [lableArray objectAtIndex:index];

}

@end


#pragma mark 

#pragma mark This is RightContentView

@implementation RightContentView


@synthesize rowNumber,rightContentDelegate,consultRightTitle;


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

       rightContentScrollView=[[UIScrollViewalloc]init];

       rightContentScrollView.delegate=self;

       rightContentScrollView.decelerationRate=0;

       rightContentScrollView.bounces=NO;

        [selfaddSubview:rightContentScrollView];

    }

   returnself;

}

- (void)dealloc {

    [rightContentScrollViewrelease];

    [rightCellArrayrelease];

    [superdealloc];

}


- (CGFloat) getWidthAt:(NSInteger)index In:(NSMutableArray * )array;

{

    if (index<[array count]) {

        CGFloat ww = [[[array objectAtIndex:index]objectForKey:@"width"]floatValue];

        if (ww==0) {

            ww = 114.5;

        }

        return ww;

    }else{

        return 0;

    }

}


//得到所有title数组的总长度

- (CGFloat)getAllWidth:(NSMutableArray *)array 

{

    CGFloat total = 0;

    for (NSInteger i=0; i<[arraycount]; i++) 

    {

        CGFloat w = [self getWidthAt:i In:array];

        if (w==0) {

            w = 114.5;

        }

        total += w;

    }

    return total;

}


-(void)cellClicked:(id)sender

{

    UIButton *temp = sender;

    NSInteger index = temp.tag;


   if (self.rightContentDelegate && [self.rightContentDelegaterespondsToSelector:@selector(didSelect:)]) {

        [self.rightContentDelegateperformSelector:@selector(didSelect:)withObject:[NSNumbernumberWithInt:index]];

    }

}


//初始化

-(void)initRightContent:(NSMutableArray *)consultRight

{

    

   rightCellArray=[[NSMutableArrayalloc]init];

    self.consultRightTitle=consultRight;

    if (rowNumber==0) {

        rowNumber=1;

    }

    

   rightContentScrollView.frame=CGRectMake(0,0,self.frame.size.width,self.frame.size.height);

   rightContentScrollView.contentSize=CGSizeMake([selfgetAllWidth:consultRightTitle],self.frame.size.height);

    

   CGFloat rowHeight=self.frame.size.height/rowNumber;

    //画出大右边的行数

    for (int i=0; i<rowNumber; i++) {

        Cell * rightCell=[[Cell alloc] initWithFrame:CGRectMake(0, rowHeight*i,0, rowHeight)andTitles:consultRightTitle];

        

        if(i%2 == 0)

        {

            rightCell.backgroundColor = [UIColorcolorWithRed:254.0/255.0green:254.0/255.0blue:254.0/255.0alpha:1.0];

        }

        else

        {

            rightCell.backgroundColor = [UIColorcolorWithRed:244.0/255.0green:244.0/255.0blue:244.0/255.0alpha:1.0];

        }

        rightCell.tag = i;

        [rightCelladdTarget:selfaction:@selector(cellClicked:)forControlEvents:UIControlEventTouchUpInside];

        

        [rightContentScrollViewaddSubview:rightCell];

    

        [rightCellArrayaddObject:rightCell];

        [rightCell release];

        

    }

}


-(UIColor *)setCellStyle:(NSString * )field andData:(NSDictionary *)dic

{

    if (self.rightContentDelegate!=nil&&[self.rightContentDelegaterespondsToSelector:@selector(setCellStyle:andData:)]) {

        return [self.rightContentDelegateperformSelector:@selector(setCellStyle:andData:)withObject:fieldwithObject:dic];

    }

   return [UIColorblackColor];

}

//填充数据

-(void)fillRightContentData:(NSMutableArray *) data;

{

    int cout=rowNumber;

    if ([data count]<rowNumber) {

        cout=[data count];

    }

    for (int i=0; i<cout; i++) {

        Cell * tempCell=[rightCellArray objectAtIndex:i];

        for (int j=0 ; j<[tempCell.lableArraycount]; j++) {

            UILabel *tempLabel=[tempCell.lableArrayobjectAtIndex:j];

            NSString * field=[[consultRightTitle objectAtIndex:j] objectForKey:@"field"];

            tempLabel.text=[[dataobjectAtIndex:i]objectForKey:field];

            tempLabel.textColor=[selfsetCellStyle:fieldandData:[dataobjectAtIndex:i]];

        }

    }

}


-(void)clearRightContentData

{

    for (int i=0; i<[rightCellArraycount]; i++) {

        Cell * tempCell=[rightCellArray objectAtIndex:i];

        for (int j=0 ; j<[tempCell.lableArraycount]; j++) {

            UILabel *tempLabel=[tempCell.lableArrayobjectAtIndex:j];

            tempLabel.text=@"";

        }

    }

}


-(void)synchronizationScrollView:(UIScrollView *)scrollview;

{

    

    CGPoint offset;

    offset.y =0;

    offset.x =floor(self.frame.size.width/scrollview.frame.size.width*scrollview.contentOffset.x);

    [rightContentScrollViewsetContentOffset:offsetanimated:NO];

    

}



// The right contentview's UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (self.rightContentDelegate!=nil&&[self.rightContentDelegaterespondsToSelector:@selector(RihgtContentDidScroll:)]) {

        [self.rightContentDelegateperformSelector:@selector(RihgtContentDidScroll:)withObject:scrollView];

    }

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{


    CGFloat addWidth=0;

   CGFloat offsetx=rightContentScrollView.contentOffset.x;

    int i =0;

   while (addWidth<rightContentScrollView.contentOffset.x) {

        addWidth=addWidth+[selfgetWidthAt:iIn:consultRightTitle];

        i++;

    }

    CGFloat currentWidth=[self getWidthAt:i In:consultRightTitle];

    if ((addWidth-offsetx)>currentWidth/2) {

        [rightContentScrollViewsetContentOffset:CGPointMake(addWidth-currentWidth,0)animated:YES];

    }else

    {

       if (addWidth+rightContentScrollView.frame.size.width>rightContentScrollView.contentSize.width) {

            [rightContentScrollViewsetContentOffset:CGPointMake(rightContentScrollView.contentSize.width-rightContentScrollView.frame.size.width,0)animated:YES];

        }else

        {

            [rightContentScrollViewsetContentOffset:CGPointMake(addWidth,0)animated:YES];

        }

    }


}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    if (!decelerate) {

        CGFloat addWidth=0;

       CGFloat offsetx=rightContentScrollView.contentOffset.x;

        int i =0;

        while (addWidth<rightContentScrollView.contentOffset.x) {

            addWidth=addWidth+[selfgetWidthAt:iIn:consultRightTitle];

            i++;

        }

        CGFloat currentWidth=[self getWidthAt:i-1 In:consultRightTitle];

        if ((addWidth-offsetx)>currentWidth/2) {

            [rightContentScrollViewsetContentOffset:CGPointMake(addWidth-currentWidth,0)animated:YES];

        }else

        {

           if (addWidth+rightContentScrollView.frame.size.width>rightContentScrollView.contentSize.width) {

                [rightContentScrollViewsetContentOffset:CGPointMake(rightContentScrollView.contentSize.width-rightContentScrollView.frame.size.width,0)animated:YES];

            }else

            {

                [rightContentScrollViewsetContentOffset:CGPointMake(addWidth,0)animated:YES];

            }

        }

    }

}

@end





#pragma mark 

#pragma mark This is LeftContentView


@implementation LeftContentView


@synthesize rowNumber,consultLeftTitle,leftContentDelegate;




- (void)dealloc {

    [leftCellArrayrelease];

    [superdealloc];

}

- (CGFloat) getWidthAt:(NSInteger)index In:(NSMutableArray * )array;

{

    if (index<[array count]) {

        CGFloat ww = [[[array objectAtIndex:index]objectForKey:@"width"]floatValue];

        if (ww==0) {

            ww = 114.5;

        }

        return ww;

    }else{

        return 0;

    }

}


//得到所有title数组的总长度

- (CGFloat)getAllWidth:(NSMutableArray *)array 

{

    CGFloat total = 0;

    for (NSInteger i=0; i<[arraycount]; i++) 

    {

        CGFloat w = [self getWidthAt:i In:array];

        if (w==0) {

            w = 114.5;

        }

        total += w;

    }

    return total;

}



-(void)cellClicked:(id)sender

{

    UIButton *temp = sender;

    NSInteger index = temp.tag;


   if (self.leftContentDelegate && [self.leftContentDelegaterespondsToSelector:@selector(didSelect:)]) {

        [self.leftContentDelegateperformSelector:@selector(didSelect:)withObject:[NSNumbernumberWithInt:index]];

    }

}

//先初始化左边内容

-(void)initLeftContent:(NSMutableArray *) consultLeft

{

    self.consultLeftTitle=consultLeft;

    

    if (rowNumber==0) {

        rowNumber=1;

    }

   leftCellArray=[[NSMutableArrayalloc]init];

   CGFloat rowHeight=self.frame.size.height/rowNumber;

    //画出大右边的行数

    for (int i=0; i<rowNumber; i++) {

        Cell * leftCell=[[Cell alloc] initWithFrame:CGRectMake(0, rowHeight*i,0, rowHeight)andTitles:consultLeftTitle];

        

        if(i%2 == 0)

        {

            leftCell.backgroundColor = [UIColorcolorWithRed:254.0/255.0green:254.0/255.0blue:254.0/255.0alpha:1.0];

        }

        else

        {

            leftCell.backgroundColor = [UIColorcolorWithRed:244.0/255.0green:244.0/255.0blue:244.0/255.0alpha:1.0];

        }

        

        leftCell.tag = i;

        [leftCelladdTarget:selfaction:@selector(cellClicked:)forControlEvents:UIControlEventTouchUpInside];

        

        [selfaddSubview:leftCell];

        [leftCellArrayaddObject:leftCell];

        [leftCell release];

        

    }

    

}

-(UIColor *)setCellStyle:(NSString * )field andData:(NSDictionary *)dic

{

    if (self.leftContentDelegate!=nil&&[self.leftContentDelegaterespondsToSelector:@selector(setCellStyle:andData:)]) {

        return [self.leftContentDelegateperformSelector:@selector(setCellStyle:andData:)withObject:fieldwithObject:dic];

    }

   return [UIColorblackColor];

}

//填充左边内容数据

-(void)fillLeftContentData:(NSMutableArray *) data

    if ([data count]<1) {

        return;

    }

    int cout=rowNumber;

    if ([data count]<rowNumber) {

        cout=[data count];

    }

    for (int i=0; i<cout; i++) {

        Cell * tempCell=[leftCellArray objectAtIndex:i];

        for (int j=0 ; j<[tempCell.lableArraycount]; j++) {

            UILabel *tempLabel=[tempCell.lableArrayobjectAtIndex:j];

            NSString * field=[[consultLeftTitle objectAtIndex:j] objectForKey:@"field"];

            tempLabel.text=[[dataobjectAtIndex:i]objectForKey:field];

            tempLabel.textColor=[selfsetCellStyle:fieldandData:[dataobjectAtIndex:i]];

        }

    }

}


-(void)clearLeftContentData

{

    for (int i=0; i<[leftCellArraycount]; i++) {

        Cell * tempCell=[leftCellArray objectAtIndex:i];

        for (int j=0 ; j<[tempCell.lableArraycount]; j++) {

            UILabel *tempLabel=[tempCell.lableArrayobjectAtIndex:j];

          

            tempLabel.text=@"";

        }

    }


}

@end





#pragma mark 

#pragma mark This is ContentView


@implementation GridContentView

@synthesize fixTitle,rowNumber,isPreFixData,contentDelegate,leftContentView,rightContentView;

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.clipsToBounds = YES;

       self.backgroundColor=[UIColorwhiteColor];


       self.leftContentView =[[[LeftContentViewalloc]init]autorelease];

       self.rightContentView=[[[RightContentViewalloc]init]autorelease];

        

       rightContentView.rightContentDelegate=self;

       leftContentView.leftContentDelegate=self;

        [selfaddSubview:leftContentView];

        [selfaddSubview:rightContentView];

        

        hxView = [[UIButton alloc] init];

       hxView.backgroundColor=[UIColorblueColor];

        hxView.alpha=0.2;

       hxView.userInteractionEnabled=NO;

       hxView.frame =CGRectZero;

        hxView.hidden = YES;

        [selfaddSubview:hxView];


    }

   returnself;

}

- (void)dealloc {

    [leftContentViewrelease];

    [rightContentViewrelease];

    [hxViewrelease];

    [superdealloc];

}


//初始化大小及宽度

-(void)initContent:(NQDataSource *)dataSource

{

    CGFloat leftTitleWidth=[dataSource getWidthCountByRangeForm:0To:fixTitle];

    CGFloat rightTitleWidth=self.frame.size.width-leftTitleWidth;

    

    leftContentView.frame=CGRectMake(0,0, leftTitleWidth,self.frame.size.height);

    rightContentView.frame=CGRectMake(leftTitleWidth,0, rightTitleWidth,self.frame.size.height);

   leftContentView.rowNumber=rowNumber;

   rightContentView.rowNumber=rowNumber;

    //分别得到左右的标题

    NSArray * lefttitlearray = [dataSource.titlessubarrayWithRange:NSMakeRange(0,fixTitle)];

    NSArray * righttitlearray = [dataSource.titlessubarrayWithRange:NSMakeRange(fixTitle, ([dataSource.titlescount]-fixTitle))];

    

    [leftContentViewinitLeftContent:[NSMutableArrayarrayWithArray:lefttitlearray]];

    [rightContentViewinitRightContent:[NSMutableArrayarrayWithArray:righttitlearray]];

}

-(void)fillContentData:(NSMutableArray *)data

{

//    if (!isPreFixData) {

    [leftContentViewfillLeftContentData:data];

//    }

    [rightContentViewfillRightContentData:data];

    

}


-(void)clearData

{

    hxView.hidden=YES;

    [leftContentViewclearLeftContentData];

    [rightContentViewclearRightContentData];

}


-(void)synchronizationScrollView:(UIScrollView *)scrollview;

{

    [rightContentViewsynchronizationScrollView:scrollview];

}


//This is rightContentDelegate

-(void)RihgtContentDidScroll:(UIScrollView *) scrollView;

{

    if (self.contentDelegate!=nil&&[self.contentDelegaterespondsToSelector:@selector(RihgtContentDidScroll:)]) {

        [self.contentDelegateperformSelector:@selector(RihgtContentDidScroll:)withObject:scrollView];

    }

}


-(UIColor *)setCellStyle:(NSString * )field andData:(NSDictionary *)dic

{NSLog(@"dic is %@",dic);

    if (self.contentDelegate!=nil&&[self.contentDelegaterespondsToSelector:@selector(setCellStyle:andData:)]) {

        return [self.contentDelegateperformSelector:@selector(setCellStyle:andData:)withObject:fieldwithObject:dic];

    }

   return [UIColorblackColor];

}


-(void)didSelect:(NSNumber *)index

{

    hxView.hidden = NO;

   hxView.frame =CGRectMake(0,(self.frame.size.height/rowNumber)*[indexintValue],self.frame.size.width,(self.frame.size.height/rowNumber));

    if (self.contentDelegate!=nil&&[self.contentDelegaterespondsToSelector:@selector(didSelect:)]) {

        [self.contentDelegateperformSelector:@selector(didSelect:)withObject:index];

    }

}


@end


#pragma mark

#pragma mark    This is RightTitleView

@interface RightTitleView :UIView 

{

    UIScrollView * rightTitleScrollView;

    NSMutableArray * rightTitleArray;

    UIButton * lastClickTitle;//最后点击的按钮

    NSInteger clickCount;//同一个的点击次数

}


@property (nonatomic ,retain) NSMutableArray * rightTitleArray;

@property (nonatomic ,assign)id rightTitleDelegate;

-(void)initRightTitle:(NSMutableArray *)rTitleArray;

-(void)synchronizationScrollView:(UIScrollView *)scrollview;

@end


@implementation RightTitleView


@synthesize rightTitleArray,rightTitleDelegate;


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        rightTitleScrollView=[[UIScrollView alloc] init];

        rightTitleScrollView.scrollEnabled=NO;

        [self addSubview:rightTitleScrollView];

    }

   returnself;

}


- (void)dealloc {

    [rightTitleScrollView release];

    [super dealloc];

}



- (CGFloat) getWidthAt:(NSInteger)index;

{

    if (index<[rightTitleArray count]) {

        CGFloat ww = [[[rightTitleArray objectAtIndex:index] objectForKey:@"width"] floatValue];

        if (ww==0) {

            ww = 114.5;

        }

        return ww;

    }else{

        return 0;

    }

}


//得到所有title数组的总长度

- (CGFloat)getAllWidth:(NSMutableArray *)array 

{

    CGFloat total = 0;

    for (NSInteger i=0; i<[array count]; i++) 

    {

        CGFloat w = [self getWidthAt:i];

        if (w==0) {

            w = 114.5;

        }

        total += w;

    }

    return total;

}


//点击右边标题时触发

-(void)rightButtonPushed:(id)sender

{

    

    UIButton *temp = sender;

    NSInteger index = temp.tag;

    NSString * isSort = [[rightTitleArray objectAtIndex:index] objectForKey:@"sort"];

    if ([isSort isEqualToString:@"NO"]) {

        return;

    }

    

    NSString * sortType=@"";

    if (![isSort isEqualToString:@"ONLY"]) {

        if (lastClickTitle!=nil) {

            NSString * lastTitle=lastClickTitle.currentTitle;

            lastTitle = [lastTitle stringByReplacingOccurrencesOfString:@"▲" withString:@""];

            lastTitle = [lastTitle stringByReplacingOccurrencesOfString:@"▼" withString:@""];

            [lastClickTitle setTitle: [NSString stringWithFormat:@"%@",lastTitle] forState:UIControlStateNormal];;

        }

        if (temp==lastClickTitle) {

            clickCount++;

        }else

        {

            clickCount=1;

        }

        

        lastClickTitle=temp;

        

        NSString *tempTitle=temp.currentTitle;

        

        tempTitle = [tempTitle stringByReplacingOccurrencesOfString:@"▲" withString:@""];

        tempTitle = [tempTitle stringByReplacingOccurrencesOfString:@"▼" withString:@""];

        


        if (clickCount % 3==1) {

           //第一次点击 倒序 esc

            tempTitle = [NSString stringWithFormat:@"%@▼",tempTitle];

            sortType=@"desc";

        }elseif(clickCount %3==2){

           //第二次点击 顺序 asc

            tempTitle = [NSString stringWithFormat:@"%@▲",tempTitle];

            sortType=@"asc";

        }else {

            // 恢复正常

        }

       

        [temp setTitle:tempTitle forState:UIControlStateNormal];

    }

   //rightTitleClick:(NSString *) field andSortType:(NSString *) sortType

   //rightTitleClick: 传出排序的字段及排序的方式

    if (self.rightTitleDelegate && [self.rightTitleDelegate respondsToSelector:@selector(rightTitleClick:andSortType:)]) {

        [self.rightTitleDelegate performSelector:@selector(rightTitleClick:andSortType:) withObject:[[rightTitleArray objectAtIndex:index] objectForKey:@"field"] withObject:sortType];

    }

}

-(void)initRightTitle:(NSMutableArray *)rTitleArray

{

    rightTitleScrollView.frame=CGRectMake(0,0,self.frame.size.width,self.frame.size.height);

    CGFloat scrollSizeWidth=[self getAllWidth:rTitleArray];

    if (scrollSizeWidth<self.frame.size.width) {

        scrollSizeWidth=self.frame.size.width;

    }

    rightTitleScrollView.contentSize=CGSizeMake(scrollSizeWidth,self.frame.size.height);

    self.rightTitleArray=rTitleArray;

    CGFloat totalWidth=0;

    for(int i = 0;i<[rTitleArray count];i++)

    {

        CGFloat width=[self getWidthAt:i];

        UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(totalWidth,0, width-10,self.frame.size.height)];

        titleButton.backgroundColor = [UIColor clearColor];

        titleButton.titleLabel.font = [UIFont systemFontOfSize:20.0f];

        titleButton.titleLabel.textColor = [UIColor whiteColor];

        [titleButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

        titleButton.tag = i;

        [titleButton addTarget:self action:@selector(rightButtonPushed:) forControlEvents:UIControlEventTouchUpInside];

        

        [titleButton setTitle:[[rTitleArray objectAtIndex:i] objectForKey:@"label"] forState:UIControlStateNormal];

        [rightTitleScrollView addSubview:titleButton];

        

        [titleButton release];

        totalWidth=totalWidth+width;

        

    }


}


-(void)synchronizationScrollView:(UIScrollView *)scrollview;

{


    CGPoint offset;

    offset.y = 0;

    offset.x = floor(self.frame.size.width/scrollview.frame.size.width*scrollview.contentOffset.x);

    [rightTitleScrollView setContentOffset:offset animated:NO];


}

@end


#pragma mark

#pragma mark This is TitleView 

@interface GridTitleView : UIView 

{

    UIButton * lastClickTitle;//最后点击的按钮

    NSInteger clickCount;//同一个的点击次数

    

}


@property (nonatomic,assign)id titleDelegate;

@property (nonatomic,assign) NSInteger fixTitle;

@property (nonatomic,retain) RightTitleView * rightTitleview;

@property (nonatomic,retain) NQDataSource * dataSource;


-(void)fillTitle:(NQDataSource *)dataSource;

-(void)synchronizationScrollView:(UIScrollView *)scrollview;


@end  


@implementation GridTitleView


@synthesize fixTitle,rightTitleview,titleDelegate,dataSource;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor=[UIColor blackColor];

        RightTitleView * tempRightTitleView=[[RightTitleView alloc] init];

        self.rightTitleview=tempRightTitleView;

        rightTitleview.rightTitleDelegate=self;

        [self addSubview:self.rightTitleview];

        [tempRightTitleView release];

    }

   returnself;

}


// This is RightTitleDelegate

-(void)rightTitleClick:(NSString *) field andSortType:(NSString *) sortType

{

    //右边标题点击了

    if (self.titleDelegate!=nil&& [self.titleDelegate respondsToSelector:@selector(rightTitleClick:andSortType:)]) {

        [self.titleDelegate performSelector:@selector(rightTitleClick:andSortType:) withObject:field withObject:sortType];

    }

    

}


-(void)leftTitleClick:(id)sender

{

    UIButton * temp=(UIButton *)sender;

    NSInteger index=temp.tag;

    

//     [titleButton setTitle:[dataSource getLabelAt:i] forState:UIControlStateNormal];

    NSString * isSort = [[self.dataSource.titles objectAtIndex:index] objectForKey:@"sort"];

    if ([isSort isEqualToString:@"NO"]) {

        return;

    }

    

    NSString * sortType=@"";

    if (![isSort isEqualToString:@"ONLY"]) {

        if (lastClickTitle!=nil) {

            NSString * lastTitle=lastClickTitle.currentTitle;

            lastTitle = [lastTitle stringByReplacingOccurrencesOfString:@"▲" withString:@""];

            lastTitle = [lastTitle stringByReplacingOccurrencesOfString:@"▼" withString:@""];

            [lastClickTitle setTitle: [NSString stringWithFormat:@"%@",lastTitle] forState:UIControlStateNormal];;

        }

        if (temp==lastClickTitle) {

            clickCount++;

        }else

        {

            clickCount=1;

        }

        

        lastClickTitle=temp;

        

        NSString *tempTitle=temp.currentTitle;

        

        tempTitle = [tempTitle stringByReplacingOccurrencesOfString:@"▲" withString:@""];

        tempTitle = [tempTitle stringByReplacingOccurrencesOfString:@"▼" withString:@""];

        

        if (clickCount % 3==1) {

           //第一次点击 倒序 esc

            tempTitle = [NSString stringWithFormat:@"%@▼",tempTitle];

            sortType=@"desc";

        }elseif(clickCount %3==2){

           //第二次点击 顺序 asc

            tempTitle = [NSString stringWithFormat:@"%@▲",tempTitle];

            sortType=@"asc";

        }else {

            // 恢复正常

        }

        [temp setTitle:tempTitle forState:UIControlStateNormal];

    

    }

    

    //左边标题点击了

    if (self.titleDelegate && [self.titleDelegate respondsToSelector:@selector(leftTitleClick:andSortType:)]) {

        [self.titleDelegate performSelector:@selector(leftTitleClick:andSortType:) withObject:[[dataSource.titles objectAtIndex:index] objectForKey:@"field"] withObject:sortType];

    }

}

-(void)fillTitle:(NQDataSource *)_dataSource

{

    self.dataSource=_dataSource;

        //初始化左边

    CGFloat totalWidth=0;

    for(int i = 0;i<fixTitle;i++)

    {

        CGFloat width=[_dataSource getWidthAt:i];

        UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(totalWidth,0, width-10,self.frame.size.height)];

        titleButton.backgroundColor = [UIColor clearColor];

        titleButton.titleLabel.font = [UIFont systemFontOfSize:20.0f];

        titleButton.titleLabel.textColor = [UIColor whiteColor];

        [titleButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

        titleButton.tag = i;

        [titleButton addTarget:self action:@selector(leftTitleClick:) forControlEvents:UIControlEventTouchUpInside];

        

         [titleButton setTitle:[_dataSource getLabelAt:i] forState:UIControlStateNormal];

        

        [self addSubview:titleButton];

        

        [titleButton release];

        totalWidth=totalWidth+width;

        

    }

   //初始化右边

    CGFloat leftTitleWidth=[_dataSource getWidthCountByRangeForm:0 To:fixTitle];

    CGFloat rightTitleWidth=self.frame.size.width-leftTitleWidth;

    self.rightTitleview.frame=CGRectMake(leftTitleWidth, 0, rightTitleWidth, self.frame.size.height);

    

    NSArray * righttitlearray = [_dataSource.titles subarrayWithRange:NSMakeRange(fixTitle, ([_dataSource.titles count]-fixTitle))];

    [rightTitleview initRightTitle:[NSMutableArray arrayWithArray:righttitlearray]];

}

-(void)synchronizationScrollView:(UIScrollView *)scrollview;

{

    [self.rightTitleview synchronizationScrollView:scrollview];

}

@end


#pragma mark

#pragma mark This is ContentPage 

//用于内容区循环翻页



@implementation ContentPage

@synthesize contentPageDelegate,fixTitle,rowNumber,isPreFixData,pageMax,scorllPage;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        self.scorllPage=[[[NQScorllPage alloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)] autorelease];

        

        scorllPage.delegate=self;

        scorllPage.scrollPageEnabled=YES;

        scorllPage.isVertical=YES;


        last=[[GridContentView alloc] init];

        next=[[GridContentView alloc] init];

        current=[[GridContentView alloc] init];

        

        last.contentDelegate=self;

        next.contentDelegate=self;

        current.contentDelegate=self;


        scorllPage.last=last;

        scorllPage.next=next;

        scorllPage.current=current;

        

        [self addSubview:scorllPage];

    }

   returnself;

}

-(void)initPage:(NQDataSource *)ds

{

    last.rowNumber=rowNumber;

    next.rowNumber=rowNumber;

    current.rowNumber=rowNumber;

    

    last.fixTitle=fixTitle;

    next.fixTitle=fixTitle;

    current.fixTitle=fixTitle;

    last.isPreFixData=isPreFixData;

    next.isPreFixData=isPreFixData;

    current.isPreFixData=isPreFixData;

//    

    [last initContent:ds];

    [next initContent:ds];

    [current initContent:ds];

    

}

-(void)fillPageData:(NSMutableArray* )data

{

    [current fillContentData:data];

    //如果固定列数据,初始化写入两页数据,


}


- (void)dealloc {

    [scorllPage release];

    [last release];

    [next release];

    [current release];

    [super dealloc];

}


//This is ContentDelegate 

-(void)RihgtContentDidScroll:(UIScrollView *) scrollView;

{

    if (self.contentPageDelegate!=nil && [self.contentPageDelegate respondsToSelector:@selector(RihgtContentDidScroll:)]) {

        [self.contentPageDelegate performSelector:@selector(RihgtContentDidScroll:) withObject:scrollView];

    }

    [last synchronizationScrollView:scrollView];

    [next synchronizationScrollView:scrollView];

//    [current synchronizationScrollView:scrollView];

}



-(UIColor *)setCellStyle:(NSString * )field andData:(NSDictionary *)dic

{

    if (self.contentPageDelegate!=nil&&[self.contentPageDelegate respondsToSelector:@selector(setCellStyle:andData:)]) {

        return [self.contentPageDelegate performSelector:@selector(setCellStyle:andData:) withObject:field withObject:dic];

    }

    return [UIColor blackColor];

}



-(void)didSelect:(NSNumber *)index

{

    if (self.contentPageDelegate!=nil && [self.contentPageDelegate respondsToSelector:@selector(didSelect:)]) {

        [self.contentPageDelegate performSelector:@selector(didSelect:) withObject:index];

    }

}


//NQScorllPageDelegate

//滑动超过一半时触发

-(void)excessHalfPageSCorllPage:(NQScorllPage*) scorllPage1 pageNumber:(NSNumber *) pagenumber;

{

    //得新调整前中后的三个视图的顺序

    last=(GridContentView *)scorllPage1.last;

    current=(GridContentView *)scorllPage1.current;

    next=(GridContentView *)scorllPage1.next;

    

    last.backgroundColor=[UIColor redColor];

    current.backgroundColor=[UIColor orangeColor];

    next.backgroundColor=[UIColor purpleColor];

  

}

//滚动到下一页时执行

-(void)scrollViewNextPage:(NQScorllPage *) scorllPage pageNumber:(NSNumber *)pagenumber;

{

    

    //判断是否预处理

    if (isPreFixData) {

        if (self.contentPageDelegate!=nil && [self.contentPageDelegate respondsToSelector:@selector(getFixTitleDatas:)]) {

            NSMutableArray * data=[self.contentPageDelegate performSelector:@selector(getFixTitleDatas:) withObject:[NSNumber numberWithInt:[pagenumber intValue]]];

            [next.leftContentView fillLeftContentData:[data objectAtIndex:0]];

            [last.rightContentView clearRightContentData];

        }

    }


}

//滚动到上一页时执行

-(void)scrollViewLastPage:(NQScorllPage *) scorllPage pageNumber:(NSNumber *)pagenumber;

{

    //判断是否预处理

    if (isPreFixData) {

        if (self.contentPageDelegate!=nil && [self.contentPageDelegate respondsToSelector:@selector(getFixTitleDatas:)]) {

            

            NSMutableArray * data1=[self.contentPageDelegate performSelector:@selector(getFixTitleDatas:) withObject:[NSNumber numberWithInt:[pagenumber intValue]]];

            [last.leftContentView fillLeftContentData:[data1 objectAtIndex:0]];

            [next.rightContentView clearRightContentData];

        }

    }


}

-(void)scrollViewMove:(NQScorllPage *)scorllPage pageNumber:(NSNumber *)pagenumber

{

    

}

-(void)scrollViewDidEnd:(NQScorllPage *)scorllPage pageNumber:(NSNumber *)pagenumber

{

    

    if (self.contentPageDelegate!=nil && [self.contentPageDelegate respondsToSelector:@selector(getContentDataPyPageNumber:)]) {

        

        [self.contentPageDelegate performSelector:@selector(getContentDataPyPageNumber:) withObject:[NSNumber numberWithInt:[pagenumber intValue]]];

    

    }

    [last clearData];

    [next clearData];

    

}


-(void)setPageMax:(NSInteger)newPageMax

{

    pageMax=newPageMax;   

    scorllPage.pageMax=pageMax;

}



@end


#pragma mark

#pragma mark This is Grid


@interface NQGrid() 


@property (nonatomic,retain) GridTitleView *titleView;


@end


@implementation NQGrid

@synthesize titleView,contentPage;

@synthesize titleHieght,contentHieght,fixTitleNumber,isPreFixData,rowNumber,maxDataNumber,dataSource,delegate;


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        

    }

   returnself;

}


- (id)initWithFrame:(CGRect)frame withTitleHeight:(NSInteger) _titleHieght

{

    self = [super initWithFrame:frame];

    if (self) {

        self.titleHieght=_titleHieght;

        CGFloat tempContentHeight=contentHieght;

        if (contentHieght==0) {

            tempContentHeight=self.frame.size.height-_titleHieght;

        }

        

        ContentPage *tempcontentPage=[[ContentPage alloc] initWithFrame:CGRectMake(0, _titleHieght,self.frame.size.width, tempContentHeight)];

        self.contentPage=tempcontentPage;

        self.contentPage.contentPageDelegate =self;

        [tempcontentPage release];

        

//        GridTitleView *tempTitleView=[[GridTitleView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,_titleHieght)];

//        self .titleView=tempTitleView;

//        self.titleView.titleDelegate=self;

//        [tempTitleView release];

//        

//        [self addSubview:titleView];

        [self addSubview:contentPage];

        

    }

   returnself;

}


- (void)dealloc {

    self.titleView=nil;

    self.contentPage=nil;

    [super dealloc];

}


//初始化Grid

-(void)initGrid:(NQDataSource *)ds

{

    self.dataSource=ds;

    

    CGFloat tempContentHeight=contentHieght;

    if (contentHieght==0) {

        tempContentHeight=self.frame.size.height-titleHieght;

    }

//

//    ContentPage *tempcontentPage=[[ContentPage alloc] initWithFrame:CGRectMake(0, titleHieght, self.frame.size.width, tempContentHeight)];

//    self.contentPage=tempcontentPage;

//    self.contentPage.contentPageDelegate =self;

//    [tempcontentPage release];

    

    GridTitleView *tempTitleView=[[GridTitleViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,titleHieght)];

    self .titleView=tempTitleView;

   self.titleView.titleDelegate=self;

    [tempTitleView release];

//    

    [selfaddSubview:titleView];

//    [self addSubview:contentPage];

    

    

   titleView.fixTitle=fixTitleNumber;//设置固定

   contentPage.fixTitle=fixTitleNumber;

   contentPage.rowNumber=rowNumber;

   contentPage.isPreFixData=isPreFixData;

    [self.contentPageinitPage:ds];

    [self.titleViewfillTitle:ds];

}


-(void)fillData:(NSMutableArray *)data

{

    [self.contentPagefillPageData:data];

}



//This is ContentDelegate 

-(void)RihgtContentDidScroll:(UIScrollView *) scrollView;

{

    [self.titleViewsynchronizationScrollView:scrollView];

}



-(UIColor *)setCellStyle:(NSString * )field andData:(NSDictionary *)dic

{

    if (self.delegate!=nil && [self.delegaterespondsToSelector:@selector(setCellStyle:andData:)]) {

        return [self.delegateperformSelector:@selector(setCellStyle:andData:)withObject:fieldwithObject:dic];

    }

   return [UIColorblackColor];

}



-(void)didSelect:(NSNumber *)index

{

    if (self.delegate!=nil && [self.delegaterespondsToSelector:@selector(didSelect:)]) {

        [self.delegateperformSelector:@selector(didSelect:)withObject:index];

    }

}


-(NSMutableArray *)getFixTitleDatas:(NSNumber* ) pageNumber

{

   NSMutableArray * data;

   if (isPreFixData) {

        if (self.delegate!=nil && [self.delegaterespondsToSelector:@selector(getFixTitleDatas:andRowNumber:)]) {

            data=[self.delegateperformSelector:@selector(getFixTitleDatas:andRowNumber:)withObject:pageNumberwithObject:[NSNumbernumberWithInt:self.rowNumber]];

        }

    }

    return data;

}


-(void)getContentDataPyPageNumber:(NSNumber* ) pageNumber

{


   if (isPreFixData) {

        if (self.delegate!=nil && [self.delegaterespondsToSelector:@selector(getContentDataPyPageNumber:andRowNumber:)]) {

            [self.delegateperformSelector:@selector(getContentDataPyPageNumber:andRowNumber:)withObject:pageNumberwithObject:[NSNumbernumberWithInt:self.rowNumber]];

        }

    }


}



// This is RightTitleDelegate

-(void)rightTitleClick:(NSString *) field andSortType:(NSString *) sortType

{

    if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(rightTitleClick:andSortType:)]) {

        [self.delegate performSelector:@selector(rightTitleClick:andSortType:) withObject:field withObject:sortType];

    }

}


-(void)leftTitleClick:(NSString *) field andSortType:(NSString *) sortType

{

    if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(leftTitleClick:andSortType:)]) {

        [self.delegate performSelector:@selector(leftTitleClick:andSortType:) withObject:field withObject:sortType];

    }

}


-(void)setMaxDataNumber:(NSInteger)newMaxDataNumber

{

    maxDataNumber=newMaxDataNumber;

    if (rowNumber!=0) {

        if(newMaxDataNumber % rowNumber == 0)

        {

            contentPage.pageMax=newMaxDataNumber / rowNumber;

        }else

            contentPage.pageMax=newMaxDataNumber / rowNumber+1;

        }

    }else

    {

        contentPage.pageMax=maxDataNumber;

    }

}


@end




第三部分:数据源

//

//  NQDataSource.h

//  IpadBlackDemo

//

//  Created by ch_soft on 11-11-17.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import<Foundation/Foundation.h>


@interface NQDataSource :NSObject

@property (nonatomic,retain)NSMutableArray * titles;

//@property (nonatomic, retain)NSMutableArray * data; 



- (CGFloat) getWidthAt:(NSInteger)index;

- (NSString *) getFieldAt:(NSInteger)index;

- (NSString *) getLabelAt:(NSInteger)index;

- (CGFloat)getAllWidth;

- (CGFloat)getWidthCountByRangeForm:(NSInteger) fromindex To:(NSInteger) toindex;


@end





//

//  NQDataSource.m

//  IpadBlackDemo

//

//  Created by ch_soft on 11-11-17.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import"NQDataSource.h"


@implementation NQDataSource


@synthesize titles;

//,data;

- (id)init

{

    self = [super init];

    if (self) {

       

    }

    

   returnself;

}



- (CGFloat) getWidthAt:(NSInteger)index;

{

    if (index<[self.titlescount]) {

       CGFloat ww = [[[self.titlesobjectAtIndex:index]objectForKey:@"width"]floatValue];

        if (ww==0) {

            ww = 114.5;

        }

        return ww;

    }else{

        return 0;

    }

}

- (NSString *) getFieldAt:(NSInteger)index;

{

    if (index<[self.titlescount]) {

       return [[self.titlesobjectAtIndex:index]objectForKey:@"field"];

    }else{

        return nil;

    }

}


- (NSString *) getLabelAt:(NSInteger)index;

{

    if (index<[self.titlescount]) {

        return [[self.titlesobjectAtIndex:index] objectForKey:@"label"];

    }else{

        return nil;

    }

}



//得到所有title数组的总长度

- (CGFloat)getAllWidth

{

    CGFloat total = 0;

    for (NSInteger i=0; i<[self.titlescount]; i++) 

    {

        CGFloat w = [self getWidthAt:i];

        if (w==0) {

            w = 114.5;

        }

        total += w;

    }

    return total;

}

//按指定的index来获取长度

- (CGFloat)getWidthCountByRangeForm:(NSInteger) fromindex To:(NSInteger) toindex;

{

    CGFloat total = 0;

    for (NSInteger i=fromindex; i<toindex; i++) 

    {

        CGFloat w = [self getWidthAt:i];

        if (w==0) {

            w = 114.5;

        }

        total += w;

    }

    return total;

}


@end



注意:文件里面还有使用我定义的翻页组件,等有空再传代码上来,有时间我也会来讲解一下代码的