UITableViewCell如何自动拉伸

1.现在xib中设置好布局

  1. 将需要的拉伸的控件拖到.m文件中,设置preferredMaxLayoutWidth属性

<pre> #import "TMContentCell.h"

@interface TMContentCell()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation TMContentCell

-(instancetype)init {
if (self = [super init]) {
self = [[NSBundle mainBundle] loadNibNamed:@"TMContentCell" owner:nil options:nil].firstObject;
}
return self;
}

-(void)awakeFromNib {
// Initialization code
self.label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 16;
// [self.label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}

@end

</code>

之后再heightForRowAtIndexPath 中设置指定cell的systemLayoutSizeFittingSize属性

<pre>
-(void)createUI {
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

CGFloat height = [[[TMContentCell alloc] init].contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 0.5;

return height;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[TMContentCell alloc] init];

return cell;

}
</pre>

demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,263评论 25 709
  • 《ijs》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 10,781评论 0 7
  • 参加集体活动,最讨厌的就是小团体行为 明明大家可以一起high,非要报团,一咕嘟一咕嘟的~~ 之前参加过两个团体活...
    tyouiki阅读 1,189评论 0 0
  • 昨天晚上9点半左右,从公司打滴滴专车回家。一上车,还没缓过神来,发现司机异常开心地对我说:你好。我连忙回应,他掩饰...
    黄子妈阅读 4,155评论 0 51