iOS 建立表格類別檢視WBDataGridView

來源:互聯網
上載者:User

iOS 建立表格類別檢視WBDataGridView

項目中建立表格, 引用標頭檔

 

#import "WBDataGridView.h"


 

- (void)viewDidLoad{


[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

 

CGFloat margin = 10.f;

CGFloat width = self.view.frame.size.width - 2*margin;

 

// - 添加表格 - 兩列

WBDataGridView *DataGrid = [[WBDataGridView alloc] initWithFrame:CGRectMake(margin, 4*margin , width, 0)

andColumnsWidths:@[@(width*0.4), @(width*0.6)]];

 

DataGrid.roundCorner = YES;

 

[DataGrid addRecord:@[@"姓名", @"dylan_lwb_"]];

[DataGrid addRecord:@[@"性別", @"男"]];

[DataGrid addRecord:@[@"電話", @"110119120"]];

[DataGrid addRecord:@[@"郵箱", @"dylan_lwb@163.com"]];

 

[self.view addSubview:DataGrid];

 

// - 添加表格 - 多列

WBDataGridView *MoreDataGrid = [[WBDataGridView alloc] initWithFrame:CGRectMake(margin, CGRectGetMaxY(DataGrid.frame) + 2*margin , width, 0)

andColumnsWidths:@[@(width*0.2), @(width*0.2), @(width*0.2), @(width*0.4)]];

 

MoreDataGrid.roundCorner = YES;

 

[MoreDataGrid addRecord:@[@"姓名", @"姓名", @"姓名", @"dylan_lwb_"]];

[MoreDataGrid addRecord:@[@"性別", @"性別", @"性別", @"男"]];

[MoreDataGrid addRecord:@[@"電話", @"電話", @"電話", @"110119120"]];

[MoreDataGrid addRecord:@[@"郵箱", @"郵箱", @"郵箱", @"dylan_lwb@163.com"]];

 

[self.view addSubview:MoreDataGrid];

}

 

 

// WBDataGridView.h

 

#import

 

extern NSString * const SwitchButtonString;

 

@interface WBDataGridView : UIView

 

@property (retain, nonatomic) NSArray *columnsWidths;

@property (assign, nonatomic) NSUInteger lastRowHeight;

@property (retain, nonatomic) UIImage *selectedImage;

@property (retain, nonatomic) UIImage *unselectedImage;

@property (assign, nonatomic) BOOL roundCorner;

 

- (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns;

- (void)addRecord:(NSArray*)record;

- (NSUInteger)selectedIndex;

 

@end

 

 

 

// WBDataGridView.m

 

#import "WBDataGridView.h"

 

NSString * const SwitchButtonString = @"SwitchButtonString";

 

@interface WBDataGridView ()

 

@property (assign, nonatomic) NSUInteger numRows;

@property (assign, nonatomic) NSUInteger dy;

@property (retain, nonatomic) NSMutableArray *switchButtons;

 

@end

 

@implementation WBDataGridView

 

- (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns{

self = [super initWithFrame:frame];

if (self)

{

self.numRows = 0;

self.columnsWidths = columns;

self.dy = 0;

self.numRows = 0;

self.switchButtons = [NSMutableArray array];

}

return self;

}

 

 

- (void)addRecord: (NSArray*)record

{

if(record.count != self.columnsWidths.count)

{

NSLog(@"!!! Number of items does not match number of columns. !!!");

return;

}

 

self.lastRowHeight = 42;

uint dx = 0;

 

NSMutableArray* labels = [NSMutableArray array];

 

// - create the items/columns of the row

for(uint i=0; i

{

float colWidth = [[self.columnsWidths objectAtIndex:i] floatValue]; //colwidth as given at setup

CGRect rect = CGRectMake(dx, self.dy, colWidth, self.lastRowHeight);

 

// - adjust X for border overlapping between columns

if(i>0)

{

rect.origin.x -= i;

}

 

NSString *oneRecord = [record objectAtIndex:i];

 

if ([oneRecord isEqualToString:SwitchButtonString])

{

// - set the switch button string as empty, create a label to adjust a cell first, then add the switch upon the label

oneRecord = @"";

}

 

UILabel* col1 = [[UILabel alloc] init];

[col1.layer setBorderColor:[[UIColor colorWithWhite:0.821 alpha:1.000] CGColor]];

[col1.layer setBorderWidth:1.0];

col1.font = [UIFont fontWithName:@"Helvetica" size:self.numRows == 0 ? 14.0f : 12.0f];

col1.textColor = [UIColor darkGrayColor];

col1.frame = rect;

 

// - round corner

if ([self isRoundCorner:i])

{

col1.layer.cornerRadius = 5;

col1.layer.masksToBounds = YES;

}

 

// - set left reght margins&alignment for the label

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

style.alignment = NSTextAlignmentCenter;

 

NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:oneRecord attributes:@{ NSParagraphStyleAttributeName : style}];

 

col1.lineBreakMode = NSLineBreakByCharWrapping;

col1.numberOfLines = 0;

col1.attributedText = attrText;

[col1 sizeToFit];

 

// - used to find height of longest label

CGFloat h = col1.frame.size.height + 10;

if(h > self.lastRowHeight){

self.lastRowHeight = h;

}

 

// - make the label width same as columns's width

rect.size.width = colWidth;

col1.frame = rect;

 

[labels addObject:col1];

 

// - used for setting the next column X position

dx += colWidth;

}

 

// - make all the labels of same height and then add to view

for(uint i=0; i

{

UILabel* tempLabel = (UILabel*)[labels objectAtIndex:i];

CGRect tempRect = tempLabel.frame;

tempRect.size.height = self.lastRowHeight;

tempLabel.frame = tempRect;

[self addSubview:tempLabel];

}

 

// - add the switch button at the first column in current row

if ([record.firstObject isEqualToString:SwitchButtonString])

{

UILabel *firstlabel = labels.firstObject;

UIButton *oneSwitchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [self.columnsWidths.firstObject integerValue], 40)];

oneSwitchButton.center = firstlabel.center;

[oneSwitchButton addTarget:self action:@selector(tapedSwitchButton:) forControlEvents:UIControlEventTouchUpInside];

[oneSwitchButton setBackgroundImage:self.selectedImage forState:UIControlStateSelected];

[oneSwitchButton setBackgroundImage:self.unselectedImage forState:UIControlStateNormal];

[self.switchButtons addObject:oneSwitchButton];

 

// - default selected first row button

if (self.switchButtons.firstObject == oneSwitchButton)

{

oneSwitchButton.selected = YES;

}

 

[self addSubview:oneSwitchButton];

}

 

self.numRows++;

 

// - adjust Y for border overlapping beteen rows

self.dy += self.lastRowHeight-1;

 

CGRect tempRect = self.frame;

tempRect.size.height = self.dy;

self.frame = tempRect;

}

 

- (void)tapedSwitchButton:(UIButton *)button

{

button.selected = !button.selected;

 

[self.switchButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

UIButton *oneButton = obj;

 

if (oneButton != button)

{

oneButton.selected = NO;

}

}];

}

 

- (NSUInteger)selectedIndex

{

__block NSUInteger index = 0;

 

[self.switchButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

UIButton *oneButton = obj;

 

if (oneButton.selected == YES)

{

index = idx;

*stop = YES;

}

}];

return index;

}

 

- (BOOL)isRoundCorner:(NSInteger)row

{

return NO;

}

 

@end




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.