這裡講述的是IOS之創立表格類視圖WBDataGridView的闡明,詳細操作請看面的內容
// Do any additional setup after loading the view.
self.view.backgroundColor= [UIColorwhiteColor];
CGFloatmargin =10.f;
CGFloatwidth =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;
[DataGridaddRecord:@[@"姓名",@"dylan_lwb_"]];
[DataGridaddRecord:@[@"性別",@"男"]];
[DataGridaddRecord:@[@"電話",@"110119120"]];
[DataGridaddRecord:@[@"郵箱",@"[email protected]"]];
[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;
[MoreDataGridaddRecord:@[@"姓名",@"姓名",@"姓名",@"dylan_lwb_"]];
[MoreDataGridaddRecord:@[@"性別",@"性別",@"性別",@"男"]];
[MoreDataGridaddRecord:@[@"電話",@"電話",@"電話",@"110119120"]];
[MoreDataGridaddRecord:@[@"郵箱",@"郵箱",@"郵箱",@"[email protected]"]];
[self.viewaddSubview:MoreDataGrid];
}
// WBDataGridView.h
#import<UIKit/UIKit.h>
externNSString*constSwitchButtonString;
@interfaceWBDataGridView :UIView
@property(retain,nonatomic)NSArray*columnsWidths;
@property(assign,nonatomic)NSUIntegerlastRowHeight;
@property(retain,nonatomic)UIImage*selectedImage;
@property(retain,nonatomic)UIImage*unselectedImage;
@property(assign,nonatomic)BOOLroundCorner;
- (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns;
- (void)addRecord:(NSArray*)record;
- (NSUInteger)selectedIndex;
@end
// WBDataGridView.m
#import"WBDataGridView.h"
NSString*constSwitchButtonString =@"SwitchButtonString";
@interfaceWBDataGridView()
@property(assign,nonatomic)NSUIntegernumRows;
@property(assign,nonatomic)NSUIntegerdy;
@property(retain,nonatomic)NSMutableArray*switchButtons;
@end
@implementationWBDataGridView
- (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];
}
returnself;
}
- (void)addRecord: (NSArray*)record
{
if(record.count!=self.columnsWidths.count)
{
NSLog(@"!!! Number of items does not match number of columns. !!!");
return;
}
self.lastRowHeight=42;
uintdx =0;
NSMutableArray* labels = [NSMutableArrayarray];
// - create the items/columns of the row
for(uinti=0; i<record.count; i++)
{
floatcolWidth = [[self.columnsWidthsobjectAtIndex:i]floatValue];//colwidth as given at setup
CGRectrect =CGRectMake(dx,self.dy, colWidth,self.lastRowHeight);
// - adjust X for border overlapping between columns
if(i>0)
{
rect.origin.x-= i;
}
NSString*oneRecord = [recordobjectAtIndex:i];
if([oneRecordisEqualToString: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.layersetBorderWidth: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;
[col1sizeToFit];
// - used to find height of longest label
CGFloath = 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;
[labelsaddObject:col1];
// - used for setting the next column X position
dx += colWidth;
}
// - make all the labels of same height and then add to view
for(uinti=0; i<labels.count; i++)
{
UILabel* tempLabel = (UILabel*)[labelsobjectAtIndex:i];
CGRecttempRect = tempLabel.frame;
tempRect.size.height=self.lastRowHeight;
tempLabel.frame= tempRect;
[selfaddSubview: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;
[oneSwitchButtonaddTarget:selfaction:@selector(tapedSwitchButton:)forControlEvents:UIControlEventTouchUpInside];
[oneSwitchButtonsetBackgroundImage:self.selectedImageforState:UIControlStateSelected];
[oneSwitchButtonsetBackgroundImage:self.unselectedImageforState:UIControlStateNormal];
[self.switchButtonsaddObject:oneSwitchButton];
// - default selected first row button
if(self.switchButtons.firstObject== oneSwitchButton)
{
oneSwitchButton.selected=YES;
}
[selfaddSubview:oneSwitchButton];
}
self.numRows++;
// - adjust Y for border overlapping beteen rows
self.dy+=self.lastRowHeight-1;
CGRecttempRect =self.frame;
tempRect.size.height=self.dy;
self.frame= tempRect;
}
- (void)tapedSwitchButton:(UIButton*)button
{
button.selected= !button.selected;
[self.switchButtonsenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {
UIButton*oneButton = obj;
if(oneButton != button)
{
oneButton.selected=NO;
}
}];
}
- (NSUInteger)selectedIndex
{
__blockNSUIntegerindex =0;
[self.switchButtonsenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {
UIButton*oneButton = obj;
if(oneButton.selected==YES)
{
index = idx;
*stop =YES;
}
}];
returnindex;
}
- (BOOL)isRoundCorner:(NSInteger)row
{
returnNO;
}
@end
【iOS之創立表格類視圖WBDataGridView】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!