iOS之创建表格类视图WBDataGridView

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

#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;

// - 添加表格 - 两列

WBDataGridView *DataGrid = [[WBDataGridViewalloc] initWithFrame:CGRectMake(margin,4*margin , width, 0)

andColumnsWidths:@[@(width*0.4),@(width*0.6)]];

DataGrid.roundCorner = YES;

[DataGrid addRecord:@[@"姓名",@"dylan_lwb_"]];

[DataGrid addRecord:@[@"性别",@"男"]];

[DataGrid addRecord:@[@"电话",@"110119120"]];

[DataGrid addRecord:@[@"邮箱",@"[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;

[MoreDataGrid addRecord:@[@"姓名",@"姓名",@"姓名",@"dylan_lwb_"]];

[MoreDataGrid addRecord:@[@"性别",@"性别",@"性别",@"男"]];

[MoreDataGrid addRecord:@[@"电话",@"电话",@"电话",@"110119120"]];

[MoreDataGrid addRecord:@[@"邮箱",@"邮箱",@"邮箱",@"[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, 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 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;

[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

时间: 2024-08-06 07:55:41

iOS之创建表格类视图WBDataGridView的相关文章

iOS 创建表格类视图WBDataGridView

项目中创建表格, 引用头文件 #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.

Django 通用类视图

引文 所有的类视图都继承django.views.generic.base.View类. 在URLconf中简单的使用通用视图 如果只是简单的做一些属性修改,可以使用as_view()方法,如下所示: from django.urls import path from django.views.generic import TemplateView urlpatterns = [ path('about/', TemplateView.as_view(template_name="about.h

IOS 创建简单表视图

创建简单表视图 此实例主要实现UITableViewDataSource协议中必须要实现的两个方法tableView:numberOfRowsInSection: 和tableView:cellForRowAtIndexPath: 当表视图显示的时候会发出tableView:numberOfRowsInSection:消息询问当前节中的行数. 当表视图单元格显示的时候会发出tableView:cellForRowAtIndexPath:消息为单元格提供显示数据. 一.实现的时序图,如下: 二.示

iOS开发——创建你自己的Framework

http://www.cocoachina.com/ios/20150127/11022.html (原文:How to Create a Framework for iOS 作者:Sam Davies 译者:Mr_cyz ) 在上一篇教程(中译版)中,你学到了怎么样创建一个可复用的圆形旋钮控件.然而你可能不清楚怎样让其他开发者更方便地去复用它. 如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这些实现你可能并不想开源出来.此

iOS开发之窗口和视图

视图就是应用程序的界面.视图可以使用nib文件实现,也可以使用代码创建.一个视图也是一个响应器(UIResponder的子类)这意味着一个视图可以与用户交互.因此,视图不只是用户可看到的界面,也是可以和用户交互的界面. 视图相关结构的名称.属性和功能 CGPoint   {x,y}  坐标信息  视图所在的坐标信息 CGSize     {width,height}   宽度和高度  视图所在的大小信息 CGRect    {origin,size}  CGPoint和CGSize的综合 视图所

ios中创建可以拖动的view原理和实现详解

有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIE

如何:从代码创建 UML 类图(ZZ)

您拖动的一个或多个类将显示在关系图上. 它们依赖的类将显示在"UML 模型资源管理器"中. 参见 模型表示类型的方式. 将程序代码中的类添加到 UML 模型 打开一个 C# 项目. 将一个 UML 类图.解决方案: 在"体系结构"菜单上,选择"新建关系图". 在"添加新关系图"对话框中选择"UML 类图". 如果您还没有,将建模项目创建. 打开"体系结构资源管理器": 在"体系

类视图

类视图 使用原则 代码越少越好 永远不要重复代码 View应当只包含呈现逻辑, 不应包括业务逻辑 保持view逻辑清晰简单 不要将CBVs用作403, 404, 500的错误处理程序 保持mixin简单明了 mixin 在编程中mixin是指为继承它的class提供额外的功能, 但它自身却不能单独使用的类 在具有多继承能力的编程语言中, mixin可以为类增加额外功能或方法. 在Django中, 我们可以使用mixin为CBVs提供更多的扩展性, 当然在类继承过程中, 我们推荐以下原则: Dja

利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO

前提条件:表sjzdfl  表sjzdxx (使用数据库MySQL) 表sjzdfl (两个字段sjzdflId 和 sjzdflmc) 表sjzdfl 建表语句: [sql] view plaincopy DROP TABLE IF EXISTS `sjzdfl`; CREATE TABLE `sjzdfl` ( `sjzdflId` int(11) NOT NULL auto_increment, `sjzdflmc` varchar(255) default NULL, PRIMARY K