iOS中创建表格类视图WBDataGridView的实例代码

  

 iOS中创建表格类视图WBDataGridView的实例代码

  

项目中创建表格,引用头文件

        #进口“WBDataGridView.h”   - (void) viewDidLoad {   [superviewDidLoad];//加载后任何额外设置视图。   self。view。写成backgroundColor=[UIColorwhiteColor];   CGFloat保证金=10.度;   CGFloat宽度=self.view.frame.size.width 2 *保证金;//砑颖砀瘛搅?   WBDataGridView * DataGrid=[[WBDataGridViewalloc] initWithFrame: CGRectMake(保证金,4 *保证金,宽度,0)   andColumnsWidths: @ @(宽* 0.4),@(宽* 0.6)]];   DataGrid。roundCorner=是的;   [DataGrid addRecord: @ @”姓名”,@ " dylan_lwb_ "]];   [DataGrid addRecord: @ @”性别”,@ "男"]];   [DataGrid addRecord: @ @”电话”,@“110119120”]];   [DataGrid addRecord: @ @”邮箱”,@“dylan_lwb@163.com”]];   [self.viewaddSubview DataGrid):;//砑颖砀瘛嗔?   WBDataGridView * MoreDataGrid=[[WBDataGridViewalloc] initWithFrame: CGRectMake(保证金,CGRectGetMaxY (DataGrid.frame) + 2 *,宽度,0)   andColumnsWidths: @ @(宽* 0.2),@(宽* 0.2),@(宽* 0.2),@(宽* 0.4)]];   MoreDataGrid。roundCorner=是的;   [MoreDataGrid addRecord: @ @”姓名”,@”姓名”,@”姓名”,@ " dylan_lwb_ "]];   [MoreDataGrid addRecord: @ @“性”别,@“性”别,@“性”别,@ "男"]];   [MoreDataGrid addRecord: @ @”电话”,@“电话”,@“电话”,@“110119120”]];   [MoreDataGrid addRecord: @ @ "邮箱”,@“邮箱”,@“邮箱”,@“dylan_lwb@163.com”]];   [self.viewaddSubview: MoreDataGrid];   }//WBDataGridView.h   # import & lt; UIKit/UIKit.h>   走读生NSString * const SwitchButtonString;   @ interface WBDataGridView: UIView   @ property(保留,原子)NSArray * columnsWidths;   @ property(分配、原子)了NSUInteger lastRowHeight;   @ property(保留,原子)界面图像* selectedImage;   @ property(保留,原子)界面图像* unselectedImage;   @ property(分配、原子)BOOL roundCorner;   - (id) initWithFrame:(CGRect中)框架andColumnsWidths: (NSArray *)列;   - (void) addRecord (NSArray *):记录;   ——(了NSUInteger) selectedIndex;   @end//WBDataGridView.m   #进口“WBDataGridView.h”   NSString * const SwitchButtonString=@“SwitchButtonString”;   @ interface WBDataGridView ()   @ property(分配、原子)了NSUInteger numRows;   @ property(分配、原子)了NSUInteger dy;   @ property(保留,原子)NSMutableArray * switchButtons;   @end   @ implementation WBDataGridView   - (id) initWithFrame:(CGRect中)框架andColumnsWidths: {(NSArray *)列   自我=(superinitWithFrame:框架);   如果(自我)   {   自我。numRows=0;   自我。columnsWidths=列;   自我。dy=0;   自我。numRows=0;   自我。switchButtons=[NSMutableArrayarray];   }   回归自我;   }   - (void) addRecord (NSArray *):记录   {   如果(记录。数!=self.columnsWidths.count)   {   NSLog (@”! ! !列数不匹配的项的数量。! ! !”);   返回;   }   自我。lastRowHeight=42;   使用uint dx=0;   NSMutableArray *标签=[NSMutableArrayarray];//- - - - - -创建条目的行/列   (使用uint我=0;i 0)   {   rect.origin。x -=我;   }   NSString * oneRecord=(记录,objectAtIndex:我);   如果([oneRecord isEqualToString: SwitchButtonString])   {//字符串为空——设置开关按钮,创建一个标签来调整细胞首先,然后添加标签上的开关   oneRecord=@ ";   }   UILabel * col1=[[UILabelalloc] init);   [col1.layersetBorderColor: [[UIColorcolorWithWhite: 0.821 alpha: 1.000] CGColor]];   [col1。层setBorderWidth: 1.0);   col1。字体=[UIFontfontWithName: @“Helvetica”大小:自我。numRows==0 & # 63;14.0 f: 12.0 f];   col1。输入textColor=[UIColordarkGrayColor];   col1.frame=矩形;//驳慕锹?   如果([selfisRoundCorner:我])   {   col1.layer。cornerRadius=5;   col1.layer。masksToBounds=是的;   }//- - - - - -设置左心中margins&校准标签   NSMutableParagraphStyle *风格=[[NSParagraphStyledefaultParagraphStyle] mutableCopy];   风格。对齐=NSTextAlignmentCenter;   NSAttributedString * attrText=[[NSAttributedStringalloc] initWithString: oneRecordattributes: @ {NSParagraphStyleAttributeName:风格});   col1。lineBreakMode=NSLineBreakByCharWrapping;   col1。numberOfLines=0;   col1。attributedText=attrText;   [col1 sizeToFit];//糜诓檎易畛け昵┑母叨?   CGFloat h=col1.frame.size.height + 10;   如果(h比;self.lastRowHeight) {   自我。lastRowHeight=h;   }//贡昵┛矶纫谎械目矶?   rect.size。宽度=colWidth;   col1.frame=矩形;   [标签addObject: col1];//糜赬位置设置在下一篇专栏文章   dx +=colWidth;   }//盟械谋昵┫嗤母叨?然后将它们添加到视图中   (使用uint我=0;i

iOS中创建表格类视图WBDataGridView的实例代码