Swift-AutoLayout system UITableViewCell
2014-10-09 19:14
351 查看
和OC 不太相同,让我纠结了一些时间,只好不用系统Cell 默认的Label了,自己创建两个Label。
step1:在自定义的cell 里面要比OC多加一句 self.contentView.layoutSubviews()
Step2:tong Obj-C
参考 OC-AutoLayout system UITableViewCell
step1:在自定义的cell 里面要比OC多加一句 self.contentView.layoutSubviews()
import UIKit class YUITCell_base: UITableViewCell { var object:AnyObject? override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func setObject(object:AnyObject){ } }
</pre><pre name="code" class="objc">import UIKit class YUITVCell_system: YUITCell_base { var headLineLbl:UILabel? var detailLbl:UILabel? override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier) //setting font--与oc运行结果不太一样,只好自定义了 // self.textLabel!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) // self.detailTextLabel!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline) // self.textLabel!.textAlignment = NSTextAlignment.Left // self.detailTextLabel!.textAlignment = NSTextAlignment.Left // self.textLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) // self.detailTextLabel!.setTranslatesAutoresizingMaskIntoConstraints(false) // self.textLabel!.numberOfLines = 0 // self.detailTextLabel!.numberOfLines = 0 // self.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping // self.detailTextLabel!.lineBreakMode = NSLineBreakMode.ByTruncatingTail headLineLbl = UILabel() as UILabel self.contentView.addSubview(headLineLbl!) detailLbl = UILabel() as UILabel self.contentView.addSubview(detailLbl!) headLineLbl!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) detailLbl!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline) headLineLbl!.textAlignment = NSTextAlignment.Left detailLbl!.textAlignment = NSTextAlignment.Left headLineLbl!.setTranslatesAutoresizingMaskIntoConstraints(false) detailLbl!.setTranslatesAutoresizingMaskIntoConstraints(false) headLineLbl!.numberOfLines = 0 detailLbl!.numberOfLines = 0 self.addCellConstraints() } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } override func setObject(object: AnyObject) { self.object = object // self.textLabel!.text = object.valueForKey("title") as? String // self.detailTextLabel!.text = object.valueForKey("brief") as? String self.headLineLbl!.text = object.valueForKey("title") as? String self.detailLbl!.text = object.valueForKey("brief") as? String } func addCellConstraints(){ var conView:UIView = self.contentView var textLbl :UIView = self.headLineLbl! var detailLbl = self.detailLbl! // var textLbl :UIView = self.textLabel! // var detailLbl = self.detailTextLabel! var viewDict = ["conView":conView,"textLbl":textLbl,"detailLbl":detailLbl] var contraints:NSMutableArray = NSMutableArray() var metrics:Dictionary = ["hPadding":10,"vPadding":8]; self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-hPadding-[textLbl]-hPadding-|", options: NSLayoutFormatOptions.AlignAllLeft, metrics: metrics, views: viewDict)) self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-hPadding-[detailLbl]-hPadding-|", options: NSLayoutFormatOptions.AlignAllLeft, metrics: metrics, views: viewDict)) self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-vPadding-[textLbl]-vPadding-[detailLbl]-vPadding-|", options: NSLayoutFormatOptions.AlignAllLeft, metrics: metrics, views: viewDict)) } override func layoutSubviews(){ super.layoutSubviews() //a must ,oc don't need this self.contentView.layoutSubviews() self.headLineLbl!.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20; self.detailLbl!.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20; // self.headLineLbl!.preferredMaxLayoutWidth = self.bounds.size.width-20; // self.detailLbl!.preferredMaxLayoutWidth = self.bounds.size.width-20; // self.textLabel!.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20; // self.detailTextLabel!.preferredMaxLayoutWidth = self.contentView.bounds.size.width-20; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */ }
Step2:tong Obj-C
_tv = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain) _tv!.rowHeight = UITableViewAutomaticDimension //UITableViewAutomaticDimension // error by 2014-10-09
_tv!.estimatedRowHeight = UITableViewAutomaticDimension _tv!.delegate = self _tv!.dataSource = self _tv!.registerClass(YUITVCell_system.self, forCellReuseIdentifier: _tvdsd!.cellReuseIdentifier) self.view.addSubview(_tv!)
参考 OC-AutoLayout system UITableViewCell
相关文章推荐
- UITableViewcell autolayout下动态高度
- UITableViewCell autolayout Unable to simultaneously satisfy constraints.
- AutoLayout实战:cell高度不固定的UITableView
- UITableViewcell autolayout下动态高度
- UITableViewCell AutoLayout 动态行高
- Autolayout uitableviewcell 自适应cell高度
- IOS 6.0+ Autolayout — UITableViewCell 高度调整
- UITableViewCell使用Autolayout布局的解决过程
- UITableViewCell使用AutoLayout自适应高度
- 结合AutoLayout实践iOS8上UITableViewCell高度的自适应
- IOS 6.0+ Autolayout — UITableViewCell 高度调整
- Swift - 使用Auto Layout和Size Classes实现页面自适应弹性布局
- swift 适配的方法autolayout 和 masory===snapKit库
- Swift: 你好, AutoLayout!
- 开源 Swift AutoLayout 框架 SnapKit 介绍
- Dynamic Table View Cell Height and Auto Layout
- iOS autolayout自适应cell高度时使用estimatedRowHeight的一些问题
- iOS 利用autolayout自定义行高仿写朋友圈界面,OC与Swift版本
- IOS autolayout自动布局实例(swift)
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights