iOS之创建表格类视图WBDataGridView
项目中创建表格, 引用头文件
#import "WBDataGridView.h"
- (void)viewDidLoad{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
CGFloat margin = .f;
CGFloat width = self.view.frame.size.width -*margin;
// - 添加表格 - 两列
WBDataGridView *DataGrid = [[WBDataGridViewalloc] initWithFrame:CGRectMake(margin,*margin , width, )
andColumnsWidths:@[@(width*0.4),@(width*0.6)]];
DataGrid.roundCorner = YES;
[DataGrid addRecord:@[@"姓名",@"dylan_lwb_"]];
[DataGrid addRecord:@[@"性别",@"男"]];
[DataGrid addRecord:@[@"电话",@""]];
[DataGrid addRecord:@[@"邮箱",@"dylan_lwb@163.com"]];
[self.viewaddSubview:DataGrid];
// - 添加表格 - 多列
WBDataGridView *MoreDataGrid = [[WBDataGridViewalloc]initWithFrame:CGRectMake(margin,CGRectGetMaxY(DataGrid.frame) +*margin , width, )
andColumnsWidths:@[@(width*0.2),@(width*0.2),@(width*0.2),@(width*0.4)]];
MoreDataGrid.roundCorner = YES;
[MoreDataGrid addRecord:@[@"姓名",@"姓名",@"姓名",@"dylan_lwb_"]];
[MoreDataGrid addRecord:@[@"性别",@"性别",@"性别",@"男"]];
[MoreDataGrid addRecord:@[@"电话",@"电话",@"电话",@""]];
[MoreDataGrid addRecord:@[@"邮箱",@"邮箱",@"邮箱",@"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 =;
self.columnsWidths = columns;
self.dy =;
self.numRows =;
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 =;
uint dx = ;
NSMutableArray* labels = [NSMutableArrayarray];
// - create the items/columns of the row
for(uint i=; 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>)
{
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:.821alpha:1.000]CGColor]];
[col1.layer setBorderWidth:1.0];
col1.font = [UIFontfontWithName:@"Helvetica"size:self.numRows == ? 14.0f :12.0f];
col1.textColor = [UIColordarkGrayColor];
col1.frame = rect;
// - round corner
if ([selfisRoundCorner:i])
{
col1.layer.cornerRadius =;
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 = ;
col1.attributedText = attrText;
[col1 sizeToFit];
// - used to find height of longest label
CGFloat h = col1.frame.size.height +;
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=; 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(,, [self.columnsWidths.firstObjectintegerValue], )];
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-;
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 =;
[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之创建表格类视图WBDataGridView的更多相关文章
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- ios动态创建类Class
[Objective-C Runtime动态加载]---动态创建类Class 动态创建类Class,动态添加Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等方法 a.使用objc_al ...
- IOS基础之 (三) 类的声明和对象的创建
一 OC类的声明和实现语法 1.接口的声明 @interface NewClassName: ParentClassName { 实例变量 ... } 方法的声明 ... @end //...表示省略 ...
- IOS 创建简单表视图
创建简单表视图 此实例主要实现UITableViewDataSource协议中必需要实现的两个方法tableView:numberOfRowsInSection: 和tableView:cellFor ...
- iOS开发——创建你自己的Framework
如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这些实现你可能并不想开源出来.此外,开发者也可能并不想看到你的所有代码,因为他们可 ...
- iOS开发之窗口和视图
视图就是应用程序的界面.视图可以使用nib文件实现,也可以使用代码创建.一个视图也是一个响应器(UIResponder的子类)这意味着一个视图可以与用户交互.因此,视图不只是用户可看到的界面,也是可以 ...
- iOS系列 基础篇 05 视图鼻祖 - UIView
iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...
- 如何:从代码创建 UML 类图(ZZ)
您拖动的一个或多个类将显示在关系图上. 它们依赖的类将显示在"UML 模型资源管理器"中. 参见 模型表示类型的方式. 将程序代码中的类添加到 UML 模型 打开一个 C# 项 ...
- 利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO
原文地址:http://blog.csdn.net/fangzhibin4712/article/details/7179414 前提条件:表sjzdfl 表sjzdxx (使用数据库MySQL) ...
随机推荐
- opencv2.4.10+VS2012配置问题
opencv2.4.10+VS2012配置 作为opencv的初学者,第一个难题想必都一样,如何配置opencv+VS的环境呢?在网上的教程,铺天盖地,但我仍然是尝试了十几次才找到属于自己的那套配置方 ...
- div居中方法总结
在日常开发过程中,我们会经常使用到div居中来处理布局,今天我就把我在开发过程中,遇到的div居中处理方法总结一下,方便日后查看! 1. 水平居中:给div设置一个宽度,然后添加marg ...
- spynner解析中文页面,应该显示中文字符的地方都是?的解决方案
这个是底层的QtWebKit相关库里 用的是Qt的QString spynner在将QString转为Python的通用字符串时,没有考虑到中文编码这一块的问题. Python27\Lib\site- ...
- Tomcat配置https之 JDK SSL证书生成与验证
关于证书 SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socket layer(SSL),安全协议是由Netscape Communication公司设计开发. ...
- ref关键字的用法
ref 关键字通过引用(而非值)传递参数. 通过引用传递的效果是,对所调用方法中的参数进行的任何更改都反映在调用方法中. 例如,如果调用方传递本地变量表达式或数组元素访问表达式,所调用方法会将对象替换 ...
- OAuth2.0 入门与进阶
一.基础知识 1.OAuth产生背景 很多网站.APP 弱化甚至没有搭建自己的账号体系,而是直接使用社会化登录的方式,这样不仅免去了用户注册账号的麻烦.还可以获取用户的好友关系来增强自身的社交功能. ...
- 21_AOP_Advice增强2(异常、引介)
[异常抛出增强] 异常抛出异常最适合的应用场景:事务管理. 当参与事务的某个Dao发生异常时,事务管理器就必须回滚事务. [异常抛出增强 例子] [操作数据库的Dao类:PersonDao.java] ...
- Android StickHeaderRecyclerView - 让recyclerview头部固定
介绍在项目中有时会需要recyclerview滑动式时某个view滑出后会固定在头部显示,比较常用的比如手机联系人界面.地区选择界面等. StickHeaderRecyclerView就是实现这个功能 ...
- SQL Server ->> CLR存储过程枚举目录文件并返回结果集
因工作需要写了个CLR存储过程枚举目录文件并返回结果集 using System; using System.IO; using System.Collections.Generic; using S ...
- C#设计模式之代理模式(二)
15.3 代理模式应用实例 下面通过一个应用实例来进一步学习和理解代理模式. 1. 实例说明 某软件公司承接了某信息咨询公司的收费商务信息查询系统的开发任务,该系统的基本需 ...