uitableView行高自定義
阿新 • • 發佈:2019-02-15
做為自己的開發筆記。所以有點簡單。
1.uitableView的基本用法和載入資料
2.就是關鍵一點了,動態演算法// // ViewController.m // 微博佈局 // // Created by 虞海飛 on 16/3/3. // Copyright © 2016年 虞海飛. All rights reserved. // #import "ViewController.h" #import "abc_TableViewCell.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *array; @end static NSString *CELL = @"abc"; @implementation ViewController -(UITableView *)tableView{ if (!_tableView) { _tableView = [UITableView new]; } return _tableView; } -(NSMutableArray *)array{ if (!_array) { _array = [NSMutableArray array]; } return _array; } - (void)viewDidLoad { [super viewDidLoad]; NSDictionary *dic_01 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"",@"label_02",nil]; NSDictionary *dic_02 = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"label_01",@"",@"label_02",nil]; NSDictionary *dic_03 = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"label_01",@"qqqqqqqqqqqqq",@"label_02",nil]; NSDictionary *dic_04 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"",@"label_02",nil]; NSDictionary *dic_05 = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"label_01",@"",@"label_02",nil]; NSDictionary *dic_06 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"",@"label_02",nil]; NSDictionary *dic_07 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"wwww",@"label_02",nil]; NSDictionary *dic_08 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"wrwrwr",@"label_02",nil]; NSDictionary *dic_09 = [NSDictionary dictionaryWithObjectsAndKeys:@"qqqqqqqqqqqqq",@"label_01",@"qqqqqqqqqqqqq",@"label_02",nil]; [self.array addObject:dic_01]; [self.array addObject:dic_02]; [self.array addObject:dic_03]; [self.array addObject:dic_04]; [self.array addObject:dic_05]; [self.array addObject:dic_06]; [self.array addObject:dic_07]; [self.array addObject:dic_08]; [self.array addObject:dic_09]; self.tableView.delegate = self; self.tableView.dataSource = self; } -(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self add_View]; } -(void) add_View{ [self.view addSubview:self.tableView]; //用自動佈局 self.tableView.translatesAutoresizingMaskIntoConstraints = NO; //top NSLayoutConstraint *top_Constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]; [self.view addConstraint:top_Constraint]; //right NSLayoutConstraint *right_Constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]; [self.view addConstraint:right_Constraint]; //left NSLayoutConstraint *left_Constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]; [self.view addConstraint:left_Constraint]; //bottom NSLayoutConstraint *bottom_Constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; [self.view addConstraint:bottom_Constraint]; } #pragma mark -- 一組多少個 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.array.count; } #pragma mark -- 多少組 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ abc_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL]; if (!cell) { cell = [[abc_TableViewCell alloc] initWithFrame:self.tableView.frame]; } NSMutableDictionary *dic = self.array[indexPath.row]; cell.dic = dic; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height; } @end
// // abc_TableViewCell.m // 微博佈局 // // Created by 虞海飛 on 16/3/3. // Copyright © 2016年 虞海飛. All rights reserved. // #import "abc_TableViewCell.h" @interface abc_TableViewCell () @property (nonatomic,strong) UILabel *label_01; @property (nonatomic,strong) UILabel *label_02; @property (nonatomic,strong) UIButton *button_01; @property (nonatomic,strong) UIButton *button_02; @end @implementation abc_TableViewCell - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }
//這個是呼叫程式碼用的方法 -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { _label_01 = [UILabel new]; _label_02 = [UILabel new]; } return self; } -(void) setDic:(NSMutableDictionary *)dic{ _dic = dic; [self add_View]; } -(void) add_View{ NSString *string_Label_01 = [self add_JudgeNull_NSString:self.dic[@"label_01"]]; if (![string_Label_01 isEqualToString:@""]) { _label_01.frame = CGRectMake(10, 10, 200, 40); } else{ _label_01.frame = CGRectMake(10, 10, 200, 0); } _label_01.backgroundColor = [UIColor redColor]; _label_01.text = string_Label_01; NSString *string_Label_02 = [self add_JudgeNull_NSString:self.dic[@"label_02"]]; if (![string_Label_02 isEqualToString:@""]) { _label_02.frame = CGRectMake(10, CGRectGetMaxY(_label_01.frame)+10, 200,40); } else{ _label_02.frame = CGRectMake(10, CGRectGetMaxY(_label_01.frame), 200, 0); } _label_02.backgroundColor = [UIColor blueColor]; _label_02.text = string_Label_02; CGRect rect = CGRectMake(0, 0, 400,CGRectGetMaxY(_label_02.frame)+10); self.frame = rect; [self addSubview:self.label_01]; [self addSubview:self.label_02]; } -(NSString *) add_JudgeNull_NSString:(id)id{ NSString *string = id; if (![string isEqualToString:@""]) { return string; } return @""; } @end
3,效果圖