iOS中创建表格类视图WBDataGridView的实例代码

 更新时间:2017年02月10日 14:04:50   作者:Jaycee麦子  
这篇文章主要介绍了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:@[@"邮箱",@"dylan_lwb@163.com"]];
  [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:@[@"邮箱",@"邮箱",@"邮箱",@"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 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

以上所述是小编给大家介绍的iOS中创建表格类视图WBDataGridView的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • IOS实现视频动画效果的启动图

    IOS实现视频动画效果的启动图

    这篇文章实现的是一个关于启动页或者引导页的视频动画效果的实现过程,对于大家开发APP具有一定的参考借鉴价值,有需要的可以来看看。
    2016-09-09
  • IOS  Swift基础之switch用法详解

    IOS Swift基础之switch用法详解

    这篇文章主要介绍了IOS Swift基础之switch用法详解的相关资料,需要的朋友可以参考下
    2017-02-02
  • iOS开发之银行卡号识别

    iOS开发之银行卡号识别

    本文给大家分享ios开发之银行卡号识别功能,思路明确,需要的朋友参考下吧
    2016-12-12
  • 浅析iOS中的浅拷贝和深拷贝(copy和mutableCopy)

    浅析iOS中的浅拷贝和深拷贝(copy和mutableCopy)

    ios提供了copy和mutablecopy方法,顾名思义,copy就是复制了一个imutable的对象,而mutablecopy就是复制了一个mutable的对象。本文给大家介绍iOS中的浅拷贝和深拷贝(copy和mutableCopy) ,感兴趣的朋友一起看看吧
    2016-04-04
  • iOS将时间NSDate转化为毫秒时间戳的方法示例

    iOS将时间NSDate转化为毫秒时间戳的方法示例

    这篇文章主要给大家介绍了关于iOS将时间NSDate转化为毫秒时间戳的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • 开发绘图、手势综合App注意点

    开发绘图、手势综合App注意点

    本篇文章主要给大家详细讲述了在IOS开发绘图、手势综合App容易遇到的坑以及注意事项等内容,有兴趣的朋友参考下吧。
    2018-02-02
  • iOS撸一个简单路由Router的实现代码

    iOS撸一个简单路由Router的实现代码

    这篇文章主要介绍了iOS撸一个简单路由Router的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 详谈iOS 位置权限弹出框闪现的问题

    详谈iOS 位置权限弹出框闪现的问题

    下面小编就为大家带来一篇详谈iOS 位置权限弹出框闪现的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • iOS实现拖拽View跟随手指浮动效果

    iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • 详解IOS串行队列与并行队列进行同步或者异步的实例

    详解IOS串行队列与并行队列进行同步或者异步的实例

    这篇文章主要介绍了详解IOS串行队列与并行队列进行同步或者异步的实例的相关资料,IOS中GCD的队列分为串行队列和并行队列,任务分为同步任务和异步任务,他们的排列组合有四种情况这里就一一分析下,需要的朋友可以参考下
    2017-07-07

最新评论