最近项目需要需要使用类似EWMultiColumnTableView这样的表格。在网上搜到一个例子,我这里给这个例子加了注释以及之前的工程,可能因版本问题不能再Xcode5上运行,我这个重写写了一遍。
本文例子下载 : http://pan.baidu.com/s/1mgHO0tU
EWMultiColumnTableView第三库下载: http://pan.baidu.com/s/1hqkMBlE
首先,将第三方库放入工程中
NSObject+NSObject_DelayedBlock.h
#import <Foundation/Foundation.h>
@interface NSObject (NSObject_DelayedBlock)
-(void)performBlock:(void (^) (void))block afterDelay:(NSTimeInterval)delay;
@end
-(void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay
{
int64_t delta = (int64_t)(1.0e9 * delay);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta), dispatch_get_main_queue(), block);
}
ViewController.h
#import <UIKit/UIKit.h>
#import "EWMultiColumnTableView.h"
@interface ViewController : UIViewController<EWMultiColumnTableViewDataSource>
{
//内容的数组
NSMutableArray* data;
NSMutableArray* sectionHeaderData;
CGFloat colWidth; //数据区每列的宽度
NSInteger numberOfSections; //数据有几个分区
NSInteger numberOfColumns; //数据的列数
EWMultiColumnTableView* tbView; //表格视图
}
@end
ViewController.m
//
// ViewController.m
// NewTableViewDemo
//
// Created by 杜甲 on 14-1-11.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "ViewController.h"
#import "NSObject+NSObject_DelayedBlock.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
numberOfColumns = 5;
numberOfSections = 5;
int sectionDistro[] = {5, 7, 4, 9, 2};
colWidth = 240.0f;
data = [[NSMutableArray alloc] init];
sectionHeaderData = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfSections; i++) {
int rows = sectionDistro[i];
NSMutableArray* a = [NSMutableArray array];
for (int j = 0; j < numberOfColumns; j++) {
int d = rand() % 100;
NSMutableString* text = [NSMutableString stringWithFormat:@"S %d C %d",i,j];
if (d < 66) {
[text appendFormat:@"\nsecond line2222"];
}
if (d < 33) {
[text appendFormat:@"\nthrid line88888"];
}
[a addObject:text];
}
[sectionHeaderData addObject:a];
NSMutableArray* sectionArray = [NSMutableArray array];
for (int k = 0; k < rows; k++) {
NSMutableArray* rowArray = [NSMutableArray array];
for (int j = 0; j < numberOfColumns; j++) {
int d = rand() % 100;
NSMutableString* text = [NSMutableString stringWithFormat:@"(%d , %d , %d)",i , k , j];
if (d < 66) {
[text appendFormat:@"\nsecond line999"];
}
if (d < 33) {
[text appendFormat:@"\nthrid line888"];
}
[rowArray addObject:text];
}
[sectionArray addObject:rowArray];
}
[data addObject:sectionArray];
}
tbView = [[EWMultiColumnTableView alloc] initWithFrame:CGRectInset(self.view.bounds, 5.0f, 5.0f)];
tbView.sectionHeaderEnabled = YES;
tbView.dataSource = self;
tbView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tbView];
[self performBlock:^{
[tbView scrollToColumn:3 position:EWMultiColumnTableViewColumnPositionMiddle animated:YES];
} afterDelay:0.5f];
}
//设置表格视图有多少个分区
-(NSInteger)numberOfSectionsInTableView:(EWMultiColumnTableView *)tableView
{
return numberOfSections;
}
//为表格视图创建一个Label标签
-(UIView*)tableView:(EWMultiColumnTableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath column:(NSInteger)col
{
UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, colWidth, 40.0f)];
l.numberOfLines = 0;
l.lineBreakMode = NSLineBreakByWordWrapping;
return l;
}
//为Cell中的Label标签赋值
-(void)tableView:(EWMultiColumnTableView *)tableView setContentForCell:(UIView *)cell indexPath:(NSIndexPath *)indexPath column:(NSInteger)col
{
UILabel* l = (UILabel*)cell;
l.text = [[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:col];
CGRect f = l.frame;
f.size.width = [self tableView:tableView widthForColumn:col];
l.frame = f;
[l sizeToFit];
}
//数据区的高度
-(CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForCellAtIndexPath:(NSIndexPath *)indexPath column:(NSInteger)column
{
NSString* str = [[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:column];
CGSize s = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake([self tableView:tableView widthForColumn:column], MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
return s.height + 20.0f;
}
//设置数据区Cell的宽度
-(CGFloat)tableView:(EWMultiColumnTableView *)tableView widthForColumn:(NSInteger)column
{
// return colWidth;
return 200;
}
//设置每个分区的个数
-(NSInteger)tableView:(EWMultiColumnTableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[data objectAtIndex:section] count];
}
//为数据区分区的头部添加一个Label
-(UIView*)tableView:(EWMultiColumnTableView *)tableView sectionHeaderCellForSection:(NSInteger)section column:(NSInteger)col
{
UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [self tableView:tableView widthForColumn:col], 40.0f)];
l.backgroundColor = [UIColor yellowColor];
return l;
}
//设置数据区分区的头部Label赋值
-(void)tableView:(EWMultiColumnTableView *)tableView setContentForSectionHeaderCell:(UIView *)cell section:(NSInteger)section column:(NSInteger)col
{
UILabel* l = (UILabel*)cell;
l.text = [NSString stringWithFormat:@"S %d C %d",section,col];
CGRect f = l.frame;
f.size.width = [self tableView:tableView widthForColumn:col];
l.frame = f;
[l sizeToFit];
}
//设置数据区分区的列数
-(NSInteger)numberOfColumnsInTableView:(EWMultiColumnTableView *)tableView
{
return numberOfColumns;
}
#pragma mark Header Cell
//为数据区左侧的Cell添加一个Label
- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellForIndexPath:(NSIndexPath *)indexPath
{
return [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 40.0f)] ;
}
//为数据区左侧的Cell的Label赋值
- (void)tableView:(EWMultiColumnTableView *)tableView setContentForHeaderCell:(UIView *)cell atIndexPath:(NSIndexPath *)indexPath
{
UILabel *l = (UILabel *)cell;
l.text = [NSString stringWithFormat:@"Line: (%d, %d)", indexPath.section, indexPath.row];
}
//设置左侧Cell的高度 为了保证左右的高度一致,所以在左侧与右侧相比较时,取最大值
- (CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForHeaderCellAtIndexPath:(NSIndexPath *)indexPath
{
return 40.0f;
}
//设置数据分区标题Cell的高度
- (CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForSectionHeaderCellAtSection:(NSInteger)section column:(NSInteger)col
{
return 50.0f;
}
//为数据分区标题添加一个Label
- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellInSectionHeaderForSection:(NSInteger)section
{
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [self widthForHeaderCellOfTableView:tableView], 30.0f)] ;
l.backgroundColor = [UIColor orangeColor];
return l;
}
//为Label的标题赋值
- (void)tableView:(EWMultiColumnTableView *)tableView setContentForHeaderCellInSectionHeader:(UIView *)cell AtSection:(NSInteger)section
{
UILabel *l = (UILabel *)cell;
l.text = [NSString stringWithFormat:@"Section %d", section];
}
/*左侧头部标题的宽度*/
- (CGFloat)widthForHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
return 200.0f;
}
/*为右侧头部标题赋值*/
- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellForColumn:(NSInteger)col
{
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, 300.0f)] ;
l.text = [NSString stringWithFormat:@"999Column: %d", col];
l.userInteractionEnabled = YES;
l.tag = col;
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] ;
recognizer.numberOfTapsRequired = 2;
[l addGestureRecognizer:recognizer];
return l;
}
/*为左侧头部标签赋值*/
- (UIView *)topleftHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, [self heightForHeaderCellOfTableView:tableView])] ;
l.text = @"Products";
return l;
}
/*总头部的高度*/
- (CGFloat)heightForHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
return 100.0f;
}
- (void)tableView:(EWMultiColumnTableView *)tableView swapDataOfColumn:(NSInteger)col1 andColumn:(NSInteger)col2
{
for (int i = 0; i < [self numberOfSectionsInTableView:tableView]; i++) {
NSMutableArray *section = [data objectAtIndex:i];
for (int j = 0; j < [self tableView:tableView numberOfRowsInSection:i]; j++) {
NSMutableArray *a = [section objectAtIndex:j];
id tmp = [a objectAtIndex:col2] ;
[a replaceObjectAtIndex:col2 withObject:[a objectAtIndex:col1]];
[a replaceObjectAtIndex:col1 withObject:tmp];
}
}
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
{
int col = [recognizer.view tag];
for (NSMutableArray *array in sectionHeaderData) {
[array removeObjectAtIndex:col];
// [array addObject:@""];
}
for (NSMutableArray *section in data) {
for (NSMutableArray *row in section) {
[row removeObjectAtIndex:col];
// [row addObject:l@""];
}
}
numberOfColumns--;
[tbView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end