iOS8中實現(xiàn)了UITableViewCell 高度自適應(yīng)方式 簡化了自適應(yīng)高度代碼。
1:初始化tableView
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor grayColor];
_tableView.estimatedRowHeight = 120;
_tableView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:_tableView];
}
這里 _tableView.estimatedRowHeight = 120;和_tableView.rowHeight = UITableViewAutomaticDimension;是實現(xiàn)自適應(yīng)高度的關(guān)鍵。第一個意思是預(yù)設(shè)高度。這個高度和實現(xiàn)的高度盡量差距小可以保證速度更快
2: 實現(xiàn)UITableViewDataSource 與 UITableViewDelegate 協(xié)議
這里是常見的實現(xiàn)方式 需要注意的是
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;?
這個方法不能實現(xiàn)。
3: 自定義Cell 實現(xiàn)自適應(yīng)高度的關(guān)鍵在 ?cell的contentView的上下左右約束必須定義。意思就是說自適應(yīng)的約束條件是依賴cell的contentView的上下左右約束。這也是其關(guān)鍵 以下為一自定義cell使用示例
#import "TBSessionTableViewCell.h"
#import "Masonry.h"
#import "TBEntity.h"
#define TagImgHead 1000
#define TagTitle 1001
#define TagDetail 1002
@implementation TBSessionTableViewCell
- (void)bindData:(TBEntity*)entity indexPath:(NSIndexPath*)indexPath {
__weak TBSessionTableViewCell *weakSelf = self;
UIImageView *imageViewHead = [self.contentView viewWithTag:TagImgHead];
if(nil == imageViewHead) {
imageViewHead = [[UIImageView alloc] init];
imageViewHead.tag = TagImgHead;
imageViewHead.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:imageViewHead];
}
imageViewHead.image = [UIImage imageNamed:@"boy_head_img"];
UILabel *labelTitel = [self.contentView viewWithTag:TagTitle];
if (nil == labelTitel) {
labelTitel? = [[UILabel alloc] init];
labelTitel.tag = TagTitle;
labelTitel.backgroundColor = [UIColor greenColor];
labelTitel.font = [UIFont systemFontOfSize:28];
[self.contentView addSubview:labelTitel];
}
labelTitel.text = entity.title;
UITextView *textView = [self.contentView viewWithTag:TagDetail];
if (nil == textView) {
textView = [[UITextView alloc] init];
textView.tag = TagDetail;
textView.font = [UIFont systemFontOfSize:22];
textView.backgroundColor = [UIColor blueColor];
textView.userInteractionEnabled = NO;
textView.scrollEnabled = NO;
[textView setShowsVerticalScrollIndicator:NO];
[textView setShowsHorizontalScrollIndicator:NO];
[self.contentView addSubview:textView];
}
textView.text = entity.detail;
[imageViewHead mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.contentView.mas_left).mas_equalTo(10);
make.top.equalTo(weakSelf.contentView.mas_top).mas_equalTo(10);
make.width.equalTo(@60);
make.height.equalTo(@60);
make.bottom.mas_lessThanOrEqualTo(@-10);//保證底部至少與圖片有10的距離
}];
[labelTitel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10);
make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(80);
make.width.mas_equalTo(weakSelf.contentView.frame.size.width-80-10);
make.height.equalTo(@28);
}];
CGFloat fW = weakSelf.contentView.frame.size.width-80-10;
//計數(shù)字高度
CGSize contentSize = [textView.text boundingRectWithSize:CGSizeMake(fW, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:22.0]}
context:nil].size;
[textView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(80);
make.top.equalTo(@58);
make.width.mas_equalTo(fW);
make.height.mas_equalTo(contentSize.height);
make.bottom.mas_lessThanOrEqualTo(@-10).priorityLow();//處理約束沖突警告保證底部10距離
}];
}