iOS8 Masonry UITableViewCell 自適應(yīng)cell高度

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距離

}];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容