Create table in project, reference header file
#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;
-Add table-two columns
Wbdatagridview *datagrid = [[Wbdatagridviewalloc] Initwithframe:cgrectmake (margin,4*margin, width, 0)
andcolumnswidths:@[@ (width*0.4), @ (width*0.6)];
Datagrid.roundcorner = YES;
[DataGrid addrecord:@[@ "name", @ "Dylan_lwb_"]];
[DataGrid addrecord:@[@ "Gender", @ "male"]];
[DataGrid addrecord:@[@ "Phone" @ "110119120"]];
[DataGrid addrecord:@[@ "email", @ "[email protected]"];
[Self.viewaddSubview:DataGrid];
-Add table-Multiple columns
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:@[@ "name", @ "name", @ "name", @ "Dylan_lwb_"]];
[Moredatagrid addrecord:@[@ "Gender", @ "gender", @ "gender", @ "male"]];
[Moredatagrid addrecord:@[@ "Phone" @ "phone" @ "phone" @ "110119120"]];
[Moredatagrid addrecord:@[@ "email", @ "Mailbox", @ "Mailbox", @ "[email protected]"];
[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 [email protected] "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 and 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 on 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 CREATE TABLE Class View Wbdatagridview