9.4 Autolayout动态改变cell高度
2019-04-25 本文已影响0人
草根小强
#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableV;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createTableView];
}
- (void)createTableView{
self.tableV = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableV.dataSource = self;
self.tableV.delegate = self;
//UITableViewAutomaticDimension让cell自适应label的高度
self.tableV.rowHeight = UITableViewAutomaticDimension;
//estimatedRowHeight 预留cell的高度
self.tableV.estimatedRowHeight = 44.0;
// self.tableV.rowHeight = 100;
[self.tableV registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"ID"];
[self.view addSubview:self.tableV];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//cell的高度是autolayout替我们算出来的
return UITableViewAutomaticDimension;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell*cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
NSArray *arr = @[@"skjdkwdjklsdkfksdlfkdsl",@"是基督教考试的年纪开始到南京上课",@"skfaksldlaksdkasdkskldjskljslkfjkljfjkl",@"wddwdwdwdwwd",@"wjwljwlkjwelkjweljrewklrekekeeeeeeeeeeeeeeeee",@"sdasdasdsadasdasds"];
cell.desLabel.text = arr[indexPath.row];
cell.titleLabel.text = self.dataArr[indexPath.row];
return cell;
}
- (NSMutableArray *)dataArr{
if (!_dataArr) {
_dataArr = [NSMutableArray arrayWithObjects:@"7834734734",@"上课了电视里快递费将诶",@"数控技术快解放你上课的减肥你上课就发生的空间发生的空间发动机开发的接口附近的设计费",@"看见看见电脑卡技能的骄傲是看你的撒娇开电脑科技等你家电脑十多年山东省大家是看得见阿什利的骄傲是劳动节爱上了大叔家看来大数据库打瞌睡的骄傲看得见阿卡的精神科",@"asdjkdjksqwjdaKSDAJKSDAJSKDHJAKSDHSA",@"无附件为IF奖围殴附件为噢附件为福建违法未划分为凤凰网i", nil];
}
return _dataArr;
}
@end
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@end
#import "MyCell.h"
@implementation MyCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
Autolayout动态改变cell高度.png