OC-AutoLayout system UITableViewCell
来源:互联网 发布:解密搜索器源码 编辑:程序博客网 时间:2024/06/10 00:40
之前计算自定义返回cell 的高度 ,现在看来实在太费事,也太坑爹。直接上代码吧
step:创建一个cell
@interface YYDynamicCell_system : UITableViewCell@property(nonatomic,strong) id object;@end
@implementation YYDynamicCell_system- (void)awakeFromNib { // Initialization code}-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier]) { self.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; self.textLabel.numberOfLines = 0; [self.textLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; // self.detailTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; self.detailTextLabel.numberOfLines = 0; self.detailTextLabel.textAlignment = NSTextAlignmentLeft; [self.detailTextLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addConstraintsToContentview]; } return self;}-(void)addConstraintsToContentview{ UIView *_conView = self.contentView; UIView *_textLabel = self.textLabel; UIView *_detailLabel = self.detailTextLabel; NSDictionary *viewDict = NSDictionaryOfVariableBindings(_conView,_textLabel,_detailLabel); NSDictionary *metrics = @{@"hPadding":@15,@"vPadding":@8}; NSMutableArray *contraints = [NSMutableArray array]; //method 01// [contraints addObject:[NSLayoutConstraint constraintWithItem:_textLabel attribute:NSLayoutAttributeFirstBaseline relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:30]];// [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_textLabel]-vPadding-[_detailLabel]" options:0 metrics:metrics views:viewDict]];//// //// [contraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:_detailLabel attribute:NSLayoutAttributeLastBaseline multiplier:1.0 constant:10]];// //这个不用//// [contraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:1 constant:44]];// ////// [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel(_detailLabel)]-hPadding-|" options:0 metrics:metrics views:viewDict]];// [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];//// [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_detailLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]];// [self.contentView addConstraints:contraints]; [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vPadding-[_textLabel]-vPadding-[_detailLabel]-vPadding-|" options:0 metrics:metrics views:viewDict]]; [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_textLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]]; [contraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hPadding-[_detailLabel]-hPadding-|" options:0 metrics:metrics views:viewDict]]; [self.contentView addConstraints:contraints]; }-(void)setObject:(id)object{ _object = object; self.textLabel.text = [object objectForKey:@"title"]; self.detailTextLabel.text = [object objectForKey:@"detail"];}-(void)layoutSubviews{ [super layoutSubviews]; //preferredMaxLayoutWidth 这个是必须要添加的,否则可能会导致显示不正常 self.textLabel.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20; self.detailTextLabel.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
_tv = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.view addSubview:_tv]; _tv.dataSource = self; _tv.delegate = self; [_tv registerClass:[YYDynamicCell_system class] forCellReuseIdentifier:CellIdentifier];
// it is important _tv.rowHeight = UITableViewAutomaticDimension;
0 0
- OC-AutoLayout system UITableViewCell
- Swift-AutoLayout system UITableViewCell
- UITableViewCell使用Autolayout
- UITableViewCell高度自适应 autoLayout
- Autolayout uitableviewcell 自适应cell高度
- UITableViewcell autolayout下动态高度
- 使用Autolayout实现自适应UITableViewCell
- 使用Autolayout定义UITableViewCell高度
- UITableViewCell使用AutoLayout自适应高度
- UITableViewCell AutoLayout 动态行高
- 使用AutoLayout动态计算UITableViewCell高度
- UITableViewCell autolayout Unable to simultaneously satisfy constraints.
- UITableViewCell使用Autolayout布局的解决过程
- ios8 Autolayout实现uitableviewcell 高度自适应
- IOS 6.0+ Autolayout — UITableViewCell 高度调整
- iOS 6.0+ Autolayout — UITableViewCell 高度调整
- 详解Autolayout 设置UITableViewCell 高度问题
- UITableViewCell高度自适应探索--AutoLayout结合Frame
- 第七周项目3-求出两个整数的正差值
- Binary Tree Maximum Path Sum [leetcode] dp
- 主站sinox.org阻塞太厉害,大家用sinox.3322.org访问
- Linux系统下如何配置SSH?如何开启SSH?
- AdaBoost算法系列1
- OC-AutoLayout system UITableViewCell
- MFC对话框,接受键盘输入,并显示输入文本
- uva 10534 Wavio Sequence 高效lis onlgn
- HDU - 4800(dp递归边界仍存疑)
- 素数距离问题(南阳24)
- 《最优化计算方法》这门课中所有的方法在回归分析的比较与分析
- SVN插件安装错误记录表
- pull方式读xml
- iOS开发asi使用(五)ASIHTTPRequest断点续传(下载)