ios可滚动模仿excel列表

来源:互联网 发布:mac带圈圈的数字怎么打 编辑:程序博客网 时间:2024/05/16 02:36

之前一个项目,需要显示列表信息,不想用tableview去做!后来想到一下的方法

新建一个类 名字为Data

.h

#import <Foundation/Foundation.h>
@interface DataSources : NSObject
{
    /**
     * 标题列表
     */
    NSMutableArray *titles;
    
    /**
     * 数据体,其中包函其它列表(NSArray)
     */
    NSMutableArray *data;
    
    /**
     * 列宽
     */
    NSMutableArray *columnWidth;
}

@property(retain) NSMutableArray *titles;
@property(retain) NSMutableArray *data;
@property(retain) NSMutableArray *columnWidth;

@end

@interface DataGridScrollViews : UIScrollView
{
    id dataGridComponent;
}
@property (nonatomic,retain)id dataGridComponent;
@end

/**
 * 数据列表组件,支持上下与左右滑动
 */
@interface Data: UIView<UIScrollViewDelegate> {
    
    //左下列视图
    DataGridScrollViews *vLeft;
    
    //右下列视图
    DataGridScrollViews *vRight;
    
    //右下列表内容
    UIView *vRightContent;
    
    //左下列表内容
    UIView *vLeftContent;
    
    //右上标题
    UIView *vTopRight;
    
    //左上标题
    UIView *vTopLeft;
    
    //列表数据源
    DataSources *dataSource;
    
    //内容总高度
    float contentHeight ;
    
    //内容总宽度
    float contentWidth;
    
    //单元格默认高度
    float cellHeight;
    
    //单元格默认宽度
    float cellWidth;
}

@property(readonly) DataGridScrollViews *vRight;
@property(readonly) DataGridScrollViews *vLeft;
@property(readwrite,nonatomic) float cellHeight;
@property(retain)     DataSources *dataSource;

/**
 * 用指定显示区域 与 数据源初始化对象
 */
- (id)initWithFrame:(CGRect)aRect data:(DataSources*)aDataSource;

@end

//
//  Data.m
//  FationWater
//
//  Created by Mobile Dev on 12-5-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Data.h"
#import "AppDelegate.h"
@implementation DataGridScrollViews
@synthesize dataGridComponent;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *t = [touches anyObject];
    Data *d = (Data*)dataGridComponent;
    AppDelegate *app=[UIApplication sharedApplication].delegate;//定义全局变量的时间
    if([t tapCount] == 1){
        int idx = [t locationInView:self].y / d.cellHeight;
       
        app.listrow=idx;
        NSLog(@"app.listrow====%d",app.listrow);
        for(int i=0;i<[d.dataSource.titles count];i++){
            for (int count=0; count<[d.dataSource.data count]; count++) {
                UILabel *l = (UILabel*)[dataGridComponent viewWithTag:count * d.cellHeight + i + 1000];
                l.alpha = 1.;
            }
        }
        for(int i=0;i<[d.dataSource.titles count];i++){
            UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
            l.alpha = .3;
        }
    }
    if([t tapCount] == 2){
        for(int i=0;i<[d.dataSource.titles count];i++){
            for (int count=0; count<[d.dataSource.data count]; count++) {
                UILabel *l = (UILabel*)[dataGridComponent viewWithTag:count * d.cellHeight + i + 1000];
                l.alpha = 1.;
                app.listrow=-1;
            }
        }
    }

}

@end

@implementation DataSources
@synthesize titles,data,columnWidth;
@end

//声明私有方法
@interface Data(Private)

/**
 * 初始化各子视图
 */
-(void)layoutSubView:(CGRect)aRect;

/**
 * 用数据项填冲数据
 */
-(void)fillData;

@end
@implementation Data
@synthesize dataSource,cellHeight,vRight,vLeft;

- (id)initWithFrame:(CGRect)aRect data:(DataSources*)aDataSource{
    self = [super initWithFrame:aRect];
    if(self != nil){
        AppDelegate *app=[UIApplication sharedApplication].delegate;//定义全局变量的时间
        app.listrow=-1;
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor grayColor];
        self.dataSource = aDataSource;
        //初始显示视图及Cell的长宽高
        contentWidth = .0;
        cellHeight = 30.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, 60, 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)];
    vLeft = [[DataGridScrollViews alloc] initWithFrame:CGRectMake(0, cellHeight, aRect.size.width, aRect.size.height - cellHeight)];
    vRight = [[DataGridScrollViews alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, contentHeight)];
    vTopRight = [[UIView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, cellHeight)];
    
    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 = [UIColor grayColor];        
    vRight.backgroundColor = [UIColor grayColor];
    vTopLeft.backgroundColor = [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 -1, cellHeight )];
        l.font = [UIFont systemFontOfSize:12.0f];
        l.text = [dataSource.titles objectAtIndex:column];
        l.backgroundColor = [UIColor grayColor];
        l.textColor = [UIColor whiteColor];
        l.textAlignment = UITextAlignmentCenter;
        
        if( 0 == column){
            [vTopLeft addSubview:l];
        }
        else{    
            [vTopRight addSubview:l];
            columnOffset += columnWidth;
        }
    }    
    
    //填冲数据内容    
    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++){
            float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];;
            UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight  , columnWidth, cellHeight -1 )];
            l.font = [UIFont systemFontOfSize:12.0f];
            l.text = [rowData objectAtIndex:column];
            l.textAlignment = UITextAlignmentCenter;
            l.tag = i * cellHeight + column + 1000;
            if(i % 2 == 0)
                l.backgroundColor = [UIColor whiteColor];
            
            if( 0 == column){
                l.frame = CGRectMake(columnOffset,  i * cellHeight , columnWidth -1 , cellHeight -1 );
                [vLeftContent addSubview:l];
            }
            else{    
                [vRightContent addSubview:l];
                columnOffset += columnWidth;
            }
        }
        
        
    }    
}


//-------------------------------以下为事件处发方法----------------------------------------
- (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);
    [self addSubview: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)
        [self scrollViewDidEndDecelerating:scrollView];
}
- (void) dealloc
{

}


@end
以上为类文件和类实现方法


方法调用

先导入Data类

    DataSources *ds = [[DataSources alloc] init];

   //设置每个表格的宽度

    ds.columnWidth = [NSArray arrayWithObjects:@"200",@"100",@"100",@"100",@"100",@"100",@"150",@"180",@"100",@"100",@"100",@"0",nil];

   //设置每个表格的标题
    ds.titles = [NSArray arrayWithObjects:@"费项",@"单元编号",@"租户",@"押金金额",@"实收押金",@"货币",@"汇率",@"单据号码",@"单据日期",@"单据条款",@"备注",@"id",nil];

//设置每个标题下的内容
    ds.data = listarray;
    Data *grid  = [[Data alloc] initWithFrame:CGRectMake(10 ,150, 990, 400) data:ds];
    [moneyview addSubview:grid];


原创粉丝点击