全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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  # 创建  # 视图webdatagridview  # 实例讲解iOS应用开发中使用UITableView创建自定义表格  # iOS自动生成表格效果的实现代码  # 小编  # 在此  # 给大家  # 所述  # 给我留言  # 感谢大家  # 疑问请  # 有任何  # 头文件  # numRows  # selectedIndex  # record  # id  # CGRect  # columns  # implementation  # superinitWithFrame  # NSMutableArrayarray  # dy  # NSMutableArray 


相关文章: 建站之星官网登录失败?如何快速解决?  制作网站的公司有哪些,做一个公司网站要多少钱?  建站之星代理如何优化在线客服效率?  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  如何选择服务器才能高效搭建专属网站?  外汇网站制作流程,如何在工商银行网站上做外汇买卖?  宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?  建站之星后台管理:高效配置与模板优化提升用户体验  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  高性价比服务器租赁——企业级配置与24小时运维服务  如何通过西部数码建站助手快速创建专业网站?  已有域名和空间如何快速搭建网站?  如何制作算命网站,怎么注册算命网站?  网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?  如何规划企业建站流程的关键步骤?  股票网站制作软件,网上股票怎么开户?  学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?  php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】  安云自助建站系统如何快速提升SEO排名?  如何快速建站并高效导出源代码?  深圳网站制作的公司有哪些,dido官方网站?  网页设计网站制作软件,microsoft office哪个可以创建网页?  免费公司网站制作软件,如何申请免费主页空间做自己的网站?  完全自定义免费建站平台:主题模板在线生成一站式服务  网站制作大概要多少钱一个,做一个平台网站大概多少钱?  枣阳网站制作,阳新火车站打的到仙岛湖多少钱?  建站之星如何通过成品分离优化网站效率?  太原网站制作公司有哪些,网约车营运证查询官网?  SAX解析器是什么,它与DOM在处理大型XML文件时有何不同?  如何正确下载安装西数主机建站助手?  建站之星导航配置指南:自助建站与SEO优化全解析  智能起名网站制作软件有哪些,制作logo的软件?  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  网站制作价目表怎么做,珍爱网婚介费用多少?  表情包在线制作网站免费,表情包怎么弄?  c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】  建站之星如何开启自定义404页面避免用户流失?  网站制作员失业,怎样查看自己网站的注册者?  XML的“混合内容”是什么 怎么用DTD或XSD定义  上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?  大同网页,大同瑞慈医院官网?  如何在Ubuntu系统下快速搭建WordPress个人网站?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  网站微信制作软件,如何制作微信链接?  子杰智能建站系统|零代码开发与AI生成SEO优化指南  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  做企业网站制作流程,企业网站制作基本流程有哪些?  大型企业网站制作流程,做网站需要注册公司吗?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。