在ViewController裏,須要有數據,即就是數組,數組
第二步 app
建立一個UITableViewCell 字體
.h文件中定義兩個屬性 分別是 一個字符串(用來賦值在TableViewcell 的Label 的Text),一個浮點數記錄TableViewcell的高atom
.m文件中 定義一個全局變量label spa
//實現這個方法3d
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifiercode
//重寫這個方法blog
1 #import <UIKit/UIKit.h> 2 3 @interface CustomTableViewCell : UITableViewCell 4 /** 5 * 顯示在cell上的字符串 6 */ 7 @property (nonatomic,copy)NSString * contentStr; 8 /** 9 * cell 高度的最大值 10 */ 11 @property (nonatomic,assign)CGFloat maxY; 12 @end
#import "CustomTableViewCell.h" @interface CustomTableViewCell () { UILabel *_label; } @end @implementation CustomTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { _label = [[UILabel alloc] init]; _label.font = [UIFont systemFontOfSize:15]; _label.numberOfLines = 0; // 換行樣式 _label.lineBreakMode = NSLineBreakByCharWrapping; [self.contentView addSubview:_label]; } return self; } - (void)setContentStr:(NSString *)contentStr { _contentStr = contentStr; // 給label賦值 _label.text = contentStr; CGFloat width = [UIScreen mainScreen].bounds.size.width - 20; // 第一個參數:字體的大小 第二個參數:就是文字顯示的區域最大值 第三個參數:文字換行的樣式 CGSize size = [_label sizeThatFits:CGSizeMake(width, MAXFLOAT)]; // CGSize size = [contentStr sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, 999) lineBreakMode:NSLineBreakByCharWrapping]; _label.frame = CGRectMake(10, 5, width, size.height); // 獲取cell的高度 _maxY = CGRectGetMaxY(_label.frame) + 10; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }
第三步 展現數據字符串
1 #import "ViewController.h" 2 #import "CustomTableViewCell.h" 3 4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 5 { 6 UITableView *_tableView; 7 NSArray *_array; 8 } 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 [self getData]; 16 [self addTableView]; 17 } 18 19 - (void)getData 20 { 21 _array = @[@"",@"",@"",@""];//此處數據本身填吧 22 } 23 24 - (void)addTableView 25 { 26 _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; 27 _tableView.delegate = self; 28 _tableView.dataSource = self; 29 [self.view addSubview:_tableView]; 30 } 31 32 33 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 34 { 35 return _array.count; 36 } 37 38 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 39 { 40 NSString *ID = @"ID"; 41 CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 42 if (cell == nil) { 43 cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 44 } 45 46 cell.contentStr = _array[indexPath.row]; 47 // 設置cell的高度 48 tableView.rowHeight = cell.maxY; 49 return cell; 50 }
須要注意的是上面代碼第48行,他在這裏爲TableViewcell 設置了Heightget
運行代碼以後的結果是