iOS中創(chuàng)建表格類視圖WBDataGridView的實(shí)例代碼
項(xiàng)目中創(chuàng)建表格, 引用頭文件
#import "WBDataGridView.h" - (void)viewDidLoad{ [superviewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColorwhiteColor]; CGFloat margin = 10.f; CGFloat width = self.view.frame.size.width -2*margin; // - 添加表格 - 兩列 WBDataGridView *DataGrid = [[WBDataGridViewalloc] 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.viewaddSubview:DataGrid]; // - 添加表格 - 多列 WBDataGridView *MoreDataGrid = [[WBDataGridViewalloc]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.viewaddSubview:MoreDataGrid]; } // WBDataGridView.h #import <UIKit/UIKit.h> 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 = [superinitWithFrame:frame]; if (self) { self.numRows =0; self.columnsWidths = columns; self.dy =0; self.numRows =0; self.switchButtons = [NSMutableArrayarray]; } 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 = [NSMutableArrayarray]; // - create the items/columns of the row for(uint i=0; i<record.count; i++) { float colWidth = [[self.columnsWidthsobjectAtIndex: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 = [[UILabelalloc] init]; [col1.layersetBorderColor:[[UIColorcolorWithWhite:0.821alpha:1.000]CGColor]]; [col1.layer setBorderWidth:1.0]; col1.font = [UIFontfontWithName:@"Helvetica"size:self.numRows ==0 ? 14.0f :12.0f]; col1.textColor = [UIColordarkGrayColor]; col1.frame = rect; // - round corner if ([selfisRoundCorner:i]) { col1.layer.cornerRadius =5; col1.layer.masksToBounds =YES; } // - set left reght margins&alignment for the label NSMutableParagraphStyle *style = [[NSParagraphStyledefaultParagraphStyle]mutableCopy]; style.alignment =NSTextAlignmentCenter; NSAttributedString *attrText = [[NSAttributedStringalloc]initWithString:oneRecordattributes:@{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<labels.count; i++) { UILabel* tempLabel = (UILabel*)[labelsobjectAtIndex: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.firstObjectisEqualToString:SwitchButtonString]) { UILabel *firstlabel = labels.firstObject; UIButton *oneSwitchButton = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, [self.columnsWidths.firstObjectintegerValue], 40)]; oneSwitchButton.center = firstlabel.center; [oneSwitchButton addTarget:selfaction:@selector(tapedSwitchButton:)forControlEvents:UIControlEventTouchUpInside]; [oneSwitchButton setBackgroundImage:self.selectedImageforState:UIControlStateSelected]; [oneSwitchButton setBackgroundImage:self.unselectedImageforState:UIControlStateNormal]; [self.switchButtonsaddObject: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.switchButtonsenumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) { UIButton *oneButton = obj; if (oneButton != button) { oneButton.selected = NO; } }]; } - (NSUInteger)selectedIndex { __block NSUInteger index =0; [self.switchButtonsenumerateObjectsUsingBlock:^(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
以上所述是小編給大家介紹的iOS中創(chuàng)建表格類視圖WBDataGridView的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
淺析iOS中的淺拷貝和深拷貝(copy和mutableCopy)
ios提供了copy和mutablecopy方法,顧名思義,copy就是復(fù)制了一個(gè)imutable的對象,而mutablecopy就是復(fù)制了一個(gè)mutable的對象。本文給大家介紹iOS中的淺拷貝和深拷貝(copy和mutableCopy) ,感興趣的朋友一起看看吧2016-04-04iOS將時(shí)間NSDate轉(zhuǎn)化為毫秒時(shí)間戳的方法示例
這篇文章主要給大家介紹了關(guān)于iOS將時(shí)間NSDate轉(zhuǎn)化為毫秒時(shí)間戳的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08iOS擼一個(gè)簡單路由Router的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS擼一個(gè)簡單路由Router的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09詳談iOS 位置權(quán)限彈出框閃現(xiàn)的問題
下面小編就為大家?guī)硪黄斦刬OS 位置權(quán)限彈出框閃現(xiàn)的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04iOS實(shí)現(xiàn)拖拽View跟隨手指浮動效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)拖拽View跟隨手指浮動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02詳解IOS串行隊(duì)列與并行隊(duì)列進(jìn)行同步或者異步的實(shí)例
這篇文章主要介紹了詳解IOS串行隊(duì)列與并行隊(duì)列進(jìn)行同步或者異步的實(shí)例的相關(guān)資料,IOS中GCD的隊(duì)列分為串行隊(duì)列和并行隊(duì)列,任務(wù)分為同步任務(wù)和異步任務(wù),他們的排列組合有四種情況這里就一一分析下,需要的朋友可以參考下2017-07-07