您的位置:首页 > 移动开发 > Swift

Swift-AutoLayout system UITableViewCell

2014-10-09 19:14 351 查看
和OC 不太相同,让我纠结了一些时间,只好不用系统Cell 默认的Label了,自己创建两个Label。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: