Create a table Class View on iOS wbdview

Source: Internet
Author: User

Create a table Class View on iOS wbdview

Create a table in the project and reference the header file

 

# Import "WBDataGridView. h"


 

-(Void) viewDidLoad {


[Super viewDidLoad];

// Do any additional setup after loading the view.

Self. view. backgroundColor = [UIColor whiteColor];

 

CGFloat margin = 10.f;

CGFloat width = self. view. frame. size. width-2 * margin;

 

//-Add Table-two columns

WBDataGridView * DataGrid = [[WBDataGridView alloc] 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. view addSubview: DataGrid];

 

//-Add a table-multiple columns

WBDataGridView * MoreDataGrid = [[WBDataGridView alloc] 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. view addSubview: MoreDataGrid];

}

 

 

// WBDataGridView. h

 

# Import

 

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 = [super initWithFrame: frame];

If (self)

{

Self. numRows = 0;

Self. columnsWidths = columns;

Self. dy = 0;

Self. numRows = 0;

Self. switchButtons = [NSMutableArray array];

}

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 = [NSMutableArray array];

 

//-Create the items/columns of the row

For (uint I = 0; I

{

Float colWidth = [[self. columnsWidths objectAtIndex: 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 = [[UILabel alloc] init];

[Col1.layer setBorderColor: [[UIColor colorWithWhite: 0.821 alpha: 1.000] CGColor];

[Col1.layer setBorderWidth: 1.0];

Col1.font = [UIFont fontWithName: @ "Helvetica" size: self. numRows = 0? 14.0f: 12.0f];

Col1.textColor = [UIColor darkGrayColor];

Col1.frame = rect;

 

//-Round corner

If ([self isRoundCorner: I])

{

Col1.layer. cornerRadius = 5;

Col1.layer. masksToBounds = YES;

}

 

//-Set left reght margins & alignment for the label

NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

Style. alignment = NSTextAlignmentCenter;

 

NSAttributedString * attrText = [[NSAttributedString alloc] initWithString: oneRecord attributes :@{ 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

{

UILabel * tempLabel = (UILabel *) [labels objectAtIndex: 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. firstObject isw.tostring: SwitchButtonString])

{

UILabel * firstlabel = labels. firstObject;

UIButton * oneSwitchButton = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, [self. columnsWidths. firstObject integerValue], 40)];

OneSwitchButton. center = firstlabel. center;

[OneSwitchButton addTarget: self action: @ selector (tapedSwitchButton :) forControlEvents: UIControlEventTouchUpInside];

[OneSwitchButton setBackgroundImage: self. selectedImage forState: UIControlStateSelected];

[OneSwitchButton setBackgroundImage: self. unselectedImage forState: UIControlStateNormal];

[Self. switchButtons addObject: 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. switchButtons enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop ){

UIButton * oneButton = obj;

 

If (oneButton! = Button)

{

OneButton. selected = NO;

}

}];

}

 

-(NSUInteger) selectedIndex

{

_ Block NSUInteger index = 0;

 

[Self. switchButtons enumerateObjectsUsingBlock: ^ (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




Related Article

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.