对NSLayoutConstraint掌握的高手求教:对于自定义tableviewCell 的时候采用这种自动布局。UIlabel换行的这种就会出现行高偏差。

LmyangBK 2014-10-04 04:36:47
对于自定义tableviewCell 的时候采用NSLayoutConstraint这种自动布局。对于大小固定死的控件布局结果不会出现偏差。但是对于UIlabel这种根据输入内容可变行高的控件。运行结果会出现偏差。
思路是:先把约束条件写好。然后通过改变每个cell的行高(行高是通过UIlabel字体内容来计算的)。来拉伸各个控件的布局。这种思路对吗?求教。
如果可以推荐一些自动布局的资料给我也行。慢慢研究。但是网上出现的这方面的资料都不怎么完整。
...全文
1864 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Albert_H 2015-10-19
  • 打赏
  • 举报
回复
楼主,问题解决了吗?怎么解决的呢?
zml5066 2014-10-11
  • 打赏
  • 举报
回复
能不能把demo共享出来,谢谢
LmyangBK 2014-10-07
  • 打赏
  • 举报
回复
#import "ViewController.h" #import "ReviewCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView *_tab; NSArray *dataSource; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createTableview]; dataSource = @[@"Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi***",@"Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi****",@"Age has reached the end of the beginning of a word.****"]; } //add tableview - (void)createTableview { NSDictionary *metrics = @{@"minPixel":@(1),}; NSDictionary *views = @{}; NSString *format = @""; NSArray *constraints = @[]; UITableView *tableview = [[UITableView alloc] init]; tableview.translatesAutoresizingMaskIntoConstraints = NO; tableview.delegate = self; tableview.dataSource = self; [self.view addSubview:tableview]; _tab = tableview; format = @"H:|-100-[tableview]-100-|"; views = NSDictionaryOfVariableBindings(tableview); constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.view addConstraints:constraints]; format = @"V:|-100-[tableview]-100-|"; constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.view addConstraints:constraints]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark UITableviewDelegate--- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height; height = 150 ; CGRect rect = [dataSource[indexPath.row] boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 200,20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12.0f] forKey:NSFontAttributeName] context:nil]; height +=rect.size.height; return height; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; ReviewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if(!cell){ cell = [[ReviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.blueLb.text = dataSource[indexPath.row]; return cell; } @end ---------------------------------------------------------------------------------------------------------------------------------------------------- #import "ReviewCell.h" @implementation ReviewCell typedef enum{ ColorLabelRedLabel, ColorLabelGreenLabel, ColorLabelBlueLabel, ColorLabelOrangeLabel }ColorLabel; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ NSDictionary *metrics = @{@"minPixel":@(1),}; NSDictionary *views = @{}; NSString *format = @""; NSArray *constraints = @[]; for (int i = 0 ;i < 4;i++) { UILabel *lb = [[UILabel alloc] init]; lb.translatesAutoresizingMaskIntoConstraints = NO; [self.contentView addSubview:lb]; switch (i) { case ColorLabelRedLabel: { lb.backgroundColor = [UIColor redColor]; // lb.frame = CGRectMake(100,15,568,30); _redLb = lb; } break; case ColorLabelGreenLabel: { //lb.frame = CGRectMake(100,55,568,30); lb.backgroundColor = [UIColor greenColor]; _greenLb = lb; } break; case ColorLabelBlueLabel: { lb.backgroundColor = [UIColor blueColor]; lb.numberOfLines = 9999 ; lb.font = [UIFont systemFontOfSize:12.0f]; lb.lineBreakMode = NSLineBreakByWordWrapping; _blueLb = lb; _blueLb.preferredMaxLayoutWidth = 768 - 200; } break; case ColorLabelOrangeLabel: { lb.backgroundColor = [UIColor orangeColor]; _orangeLb = lb; } break; } } //_redLb format = @"H:|-100-[_redLb]-100-|"; views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb); constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; //_greenLb format = @"H:|-100-[_greenLb]-100-|"; views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb); constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; // _blueLb format = @"H:|-100-[_blueLb]-100-|"; views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb); constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; //_orangeLb format = @"H:|-100-[_orangeLb]-100-|"; views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb); constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; // format = @"V:|-15-[_redLb(30)]-10-[_greenLb(40)]-10-[_blueLb]-10-[_orangeLb(20)]-15-|"; // constraints =[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; // [self.contentView addConstraints:constraints]; format = @"V:|-15-[_redLb(30)]-10-[_greenLb(40)]-10-[_blueLb]"; constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; format = @"V:[_orangeLb(20)]-15-|"; constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views]; [self.contentView addConstraints:constraints]; [self needsUpdateConstraints]; } return self; } - (void)setBlueLb:(UILabel *)blueLb { } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end --------------------------------------------------------------------------------------------------------------------------------------------------- 思路是在ReviewCell 中添加约束。然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 中改变cell的高度。然后用于拉伸出_blueLb的高度。
Bannings 2014-10-06
  • 打赏
  • 举报
回复
你不能在heightForRow里取cell,这样会造成死循环的。你就算把约束条件写好,也只能使Label的高度和cell的高度自适应,但是cell除了要将自己的高度自适应以外,还要在TableView的delegate里返回正确的高度,这是你的约束解决不了的情况,要改变cell的高度,只能通过reload cell,然后在heightForRow里返回正确的高度。 在自定义的Cell里公开一个类方法作为接口,这个接口接收一个数据源(比如你的Label的text),在这个类方法里计算高度并返回。

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧