Create a table Class View for iOS wbdview dview, ioswbdatagri

Source: Internet
Author: User

Create a table Class View for iOS wbdview dview, ioswbdatagri

Create a table in the project and reference the 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: @ [@ "Mailbox", @ "dylan_lwb@163.com"];

[Self. viewaddSubview: DataGrid];

//-Add a 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", @ "dylan_lwb _"];

[MoreDataGrid addRecord: @ [@ "gender", @ "male"];

[MoreDataGrid addRecord: @ [@ "phone", @ "110119120"];

[MoreDataGrid addRecord: @ [@ "Mailbox", @ "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 isw.tostring: 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.821 alpha: 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. firstobjectis#tostring: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.