UITableViewCell自定义高度
生活随笔
收集整理的這篇文章主要介紹了
UITableViewCell自定义高度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一步?
在ViewController里,需要有數據,即就是數組,
第二步 ?
? ?創建一個UITableViewCell?
? ?.h文件中定義兩個屬性 分別是 一個字符串(用來賦值在TableViewcell 的Label 的Text),一個浮點數記錄TableViewcell的高
? ?.m文件中 ?定義一個全局變量label ?
//實現這個方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//重寫這個方法
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 設置了Height
運行代碼之后的結果是
?
轉載于:https://www.cnblogs.com/fanwenzheIOS/p/4978825.html
總結
以上是生活随笔為你收集整理的UITableViewCell自定义高度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测试技术网站分享
- 下一篇: 生产服务器环境最小化安装后Centos