iOS中创建表格类视图WBDataGridView的实例代码-创新互联

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

创新互联公司是一家专注于成都做网站、成都网站制作与策划设计,威宁网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:威宁等地区。威宁做网站价格咨询:028-86922220

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

#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 
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; i0)
    {
      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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:iOS中创建表格类视图WBDataGridView的实例代码-创新互联
网页URL:http://abwzjs.com/article/dhgepc.html