|
| 1 | +// |
| 2 | +// TLT3ViewController.m |
| 3 | +// MyLayout |
| 4 | +// |
| 5 | +// Created by oybq on 15/7/18. |
| 6 | +// Copyright (c) 2015年 YoungSoft. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "TLTest3ViewController.h" |
| 10 | +#import "MyLayout.h" |
| 11 | + |
| 12 | +@interface TLTest3ViewController () |
| 13 | + |
| 14 | +@end |
| 15 | + |
| 16 | +@implementation TLTest3ViewController |
| 17 | + |
| 18 | + |
| 19 | +-(void)loadView |
| 20 | +{ |
| 21 | + /* |
| 22 | + 这个例子是将表格布局和智能边界线的应用结合,实现一个表格界面。 |
| 23 | + |
| 24 | + */ |
| 25 | + [super loadView]; |
| 26 | + |
| 27 | + UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)]; |
| 28 | + scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; |
| 29 | + [self.view addSubview:scrollView]; |
| 30 | + |
| 31 | + //建立一个垂直表格 |
| 32 | + MyTableLayout *tableLayout = [MyTableLayout tableLayoutWithOrientation:MyLayoutViewOrientation_Vert]; |
| 33 | + tableLayout.myLeftMargin = tableLayout.myRightMargin = 0; //宽度和非布局父视图一样宽 |
| 34 | + [scrollView addSubview:tableLayout]; |
| 35 | + |
| 36 | + |
| 37 | + //建立一个表格外边界的边界线。颜色为黑色,粗细为3. |
| 38 | + MyBorderLineDraw *outerBorderLine = [[MyBorderLineDraw alloc] initWithColor:[UIColor blackColor]]; |
| 39 | + outerBorderLine.thick = 3; |
| 40 | + tableLayout.boundBorderLine = outerBorderLine; |
| 41 | + |
| 42 | + //建立智能边界线。所谓智能边界线就是布局里面的如果有子布局视图,则子布局视图会根据自身的布局位置智能的设置边界线。 |
| 43 | + //智能边界线只支持表格布局、线性布局、流式布局、浮动布局。 |
| 44 | + //如果要想完美使用智能分界线,则请将cellview建立为一个布局视图,比如本例子中的createCellLayout。 |
| 45 | + MyBorderLineDraw *innerBorderLine = [[MyBorderLineDraw alloc] initWithColor:[UIColor redColor]]; |
| 46 | + tableLayout.IntelligentBorderLine = innerBorderLine; |
| 47 | + |
| 48 | + |
| 49 | + //添加第一行。行高为50,每列宽由自己确定。 |
| 50 | + MyLinearLayout *firstRow = [tableLayout addRow:50 colSize:MTLSIZE_MATCHPARENT]; |
| 51 | + firstRow.notUseIntelligentBorderLine = YES; //因为智能边界线会影响到里面的所有子布局,包括每行,但是这里我们希望这行不受智能边界线的影响而想自己定义边界线,则将这个属性设置为YES。 |
| 52 | + firstRow.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:[UIColor blueColor]]; //我们自定义第一行的底部边界线为蓝色边界线。 |
| 53 | + |
| 54 | + NSArray *firstRowTitles = @[@"姓名",@"周一",@"周二",@"周三", @"周四",@"周五",@"周六",@"周日"]; |
| 55 | + [firstRowTitles enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * stop) { |
| 56 | + |
| 57 | + UIView *cellView = [self createCellLayout:obj]; //这里为什么要用布局视图作为单元格视图,是因为智能边界线只会影响到布局子视图,非布局子视图是没有智能边界线的。 |
| 58 | + if (idx == 0) |
| 59 | + cellView.myWidth = 80; |
| 60 | + else |
| 61 | + cellView.weight = 1; //我们这里定义第一列的宽度为80,而其他的列宽平均分配。 |
| 62 | + |
| 63 | + [tableLayout addSubview:cellView]; //表格布局重写了addSubview,表示总是添加到最后一行上。 |
| 64 | + }]; |
| 65 | + |
| 66 | + |
| 67 | + NSArray *names = @[@"欧阳大哥",@"周杰",@"{丸の子}",@"小鱼",@"Sarisha゛"]; |
| 68 | + NSArray *values = @[@"", @"10",@"20"]; |
| 69 | + |
| 70 | + //建立10行的数据。 |
| 71 | + for (int i = 0; i < 10; i++) |
| 72 | + { |
| 73 | + [tableLayout addRow:40 colSize:MTLSIZE_MATCHPARENT]; //添加新的一行。 |
| 74 | + |
| 75 | + for (int j = 0; j < firstRowTitles.count; j++) |
| 76 | + { |
| 77 | + UIView *cellView = nil; |
| 78 | + if (j == 0) |
| 79 | + { |
| 80 | + cellView = [self createCellLayout:names[arc4random_uniform((uint32_t)names.count)]]; |
| 81 | + cellView.myWidth = 80; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + cellView = [self createCellLayout:values[arc4random_uniform((uint32_t)values.count)]]; |
| 86 | + cellView.weight = 1; |
| 87 | + } |
| 88 | + |
| 89 | + [tableLayout addSubview:cellView]; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + //最后一行: |
| 94 | + MyLinearLayout *lastRow = [tableLayout addRow:60 colSize:MTLSIZE_MATCHPARENT]; |
| 95 | + lastRow.notUseIntelligentBorderLine = YES; |
| 96 | + lastRow.topBorderLine = [[MyBorderLineDraw alloc] initWithColor:[UIColor greenColor]]; |
| 97 | + |
| 98 | + UIView *cellLayout = [self createCellLayout:@"合计:"]; |
| 99 | + cellLayout.weight = 1; //占用剩余宽度 |
| 100 | + [tableLayout addSubview:cellLayout]; |
| 101 | + |
| 102 | + cellLayout = [self createCellLayout:@"$1234.11"]; |
| 103 | + cellLayout.myWidth = 100; //固定宽度。 |
| 104 | + [tableLayout addSubview:cellLayout]; |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +- (void)viewDidLoad { |
| 110 | + [super viewDidLoad]; |
| 111 | + // Do any additional setup after loading the view. |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +- (void)didReceiveMemoryWarning { |
| 116 | + [super didReceiveMemoryWarning]; |
| 117 | + // Dispose of any resources that can be recreated. |
| 118 | +} |
| 119 | + |
| 120 | +#pragma mark -- Layout Construction |
| 121 | + |
| 122 | +-(MyFrameLayout*)createCellLayout:(NSString*)value |
| 123 | +{ |
| 124 | + MyFrameLayout *cellLayout = [MyFrameLayout new]; |
| 125 | + |
| 126 | + UILabel *label = [UILabel new]; |
| 127 | + label.text = value; |
| 128 | + label.adjustsFontSizeToFitWidth = YES; |
| 129 | + label.textAlignment = NSTextAlignmentCenter; |
| 130 | + label.font = [UIFont systemFontOfSize:14]; |
| 131 | + label.marginGravity = MyMarginGravity_Fill; |
| 132 | + [cellLayout addSubview:label]; |
| 133 | + |
| 134 | + return cellLayout; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +/* |
| 139 | +#pragma mark - Navigation |
| 140 | +
|
| 141 | +// In a storyboard-based application, you will often want to do a little preparation before navigation |
| 142 | +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { |
| 143 | + // Get the new view controller using [segue destinationViewController]. |
| 144 | + // Pass the selected object to the new view controller. |
| 145 | +} |
| 146 | +*/ |
| 147 | + |
| 148 | +@end |
0 commit comments