Swift-AutoLayout system UITableViewCell

18 篇文章 0 订阅

和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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值