swift cell的高度是动态的 三个文件:控制器 cell Frame类
說明: ?在控制器中 申明frames的數組 存入frame模型,在調用cell的時候傳入的是 frame,在frame中存有 高度變量 和 cell子控件的frame變量 ?和 ?模型數據變量 ,在cell中創建子控件,并用frame復值
import UIKit
class TTFaq : NSObject{
? ? override init(){
? ? ? ? super.init()
? ? }
?? ?
? ? var txtq : String = String()
? ? var txta : String = String()
}
class TTFaqFrame: NSObject {
? ? let padding: CGFloat = 10
? ? var qF: CGRect = CGRectZero
? ? var aF: CGRect = CGRectZero
? ? var backF: CGRect = CGRectZero
? ? var cellHeight: CGFloat = 0
? ? let maxF = CGFloat(MAXFLOAT)
? ? let qW : CGFloat = ScreenWidth - 48
? ? var faq : TTFaq? = nil{
? ? ? ? didSet{
? ? ? ? ? ? // Q? 問題
? ? ? ? ? ? let qX : CGFloat = 16
? ? ? ? ? ? let qY : CGFloat = 20
? ? ? ? ? ? let qSize = faq!.txtq.boundingRectWithSize(CGSizeMake(qW, maxF), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont .systemFontOfSize(12)], context: nil).size
? ? ? ? ? ? qF = CGRectMake(qX, qY, qSize.width, qSize.height)
?? ? ? ? ? ?
? ? ? ? ? ? // A 答案
? ? ? ? ? ? let aX : CGFloat = 16
? ? ? ? ? ? let aY : CGFloat = CGRectGetMaxY(qF) + 20
? ? ? ? ? ? let aSize = faq!.txta.boundingRectWithSize(CGSizeMake(qW, maxF), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12)], context: nil).size
? ? ? ? ? ? aF = CGRectMake(aX, aY,aSize.width,aSize.height)
?? ? ? ? ? ?
? ? ? ? ? ? let backH = CGRectGetMaxY(aF) + 20
? ? ? ? ? ? cellHeight = CGRectGetMaxY(aF) + 32
? ? ? ? ? ? backF = CGRectMake(8,12,ScreenWidth - 16 ,backH)
? ? ? ? }
?? ?
? ? }
?? ?
?? ?
}
import UIKit
class TTFaqTableViewCell: UITableViewCell {
? ? var lblq: UILabel = UILabel()
? ? var lbla: UILabel = UILabel()
? ? var backView: UIView = UIView ()
? ? var faqF: TTFaqFrame = TTFaqFrame(){
? ? ? ? didSet{
? ? ? ? ? ? let faq: TTFaq = faqF.faq!
? ? ? ? ? ? lblq.frame = faqF.qF
? ? ? ? ? ? lblq.text = faq.txtq as String
? ? ? ? ? ? lbla.frame = faqF.aF
? ? ? ? ? ? lbla.text = faq.txta as String
? ? ? ? ? ? backView.frame = faqF.backF
? ? ? ? }
? ? }
? ? override func awakeFromNib() {
? ? ? ? super.awakeFromNib()
?? ? ? ?
? ? }
? ? override func setSelected(selected: Bool, animated: Bool) {
? ? ? ? super.setSelected(selected, animated: animated)
? ? ? ? // Configure the view for the selected state
? ? }
?? ?
? ? override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
?? ? ? ?
? ? ? ? super.init(style: style, reuseIdentifier: reuseIdentifier)
?? ? ? ?
? ? ? ? backView.backgroundColor = UIColor.whiteColor()
? ? ? ? backView.layer.cornerRadius = 8
? ? ? ? self.contentView.addSubview(backView)
?? ? ? ?
? ? ? ? //1. Q
? ? ? ? lblq.textAlignment = NSTextAlignment.Left
? ? ? ? lblq.font = UIFont .systemFontOfSize(12.0)
? ? ? ? lblq.textColor = TTColor("#27a9e3")
? ? ? ? lblq.numberOfLines = 0
? ? ? ? self.backView.addSubview(lblq)
?? ? ? ?
? ? ? ? //2. A
? ? ? ? lbla.textAlignment = NSTextAlignment.Left
? ? ? ? lbla.font = UIFont.systemFontOfSize(12.0)
? ? ? ? lbla.textColor = TTColor("#666666")
? ? ? ? lbla.numberOfLines = 0
? ? ? ? self.backView.addSubview(lbla)
? ? ? ? self.contentView.backgroundColor = grayColor
? ? }
?? ?
? ? required init(coder aDecoder: NSCoder) {
? ? ? ? fatalError("init(coder:) has not been implemented")
? ? }
?? ?
? ? //MARK:- Delegate or DataSource
?? ?
? ? //MARK:- NSNotification Method
?? ?
? ? //MARK:- Action Method
?? ?
? ? //MARK:- Private Method
}
import UIKit
class TTFaqTableViewController: UIViewController,TTInterceptorProtocol,UITableViewDataSource,UITableViewDelegate{
?? ?
? ? var faqFrames : NSMutableArray = {
? ? ? ? var arrayFAQ : NSMutableArray = NSMutableArray()
? ? ? ? for var index = 0; index < 5 ; index++ {
? ? ? ? ? ? var tmpF : TTFaqFrame = TTFaqFrame()
? ? ? ? ? ? var tmpfaq : TTFaq = TTFaq()
? ? ? ? ? ? tmpfaq.txtq = "Q:The red light flashes when the APP is searching for wukong i8.The red light flashes when the APP is searching for wukong i8.The red light flashes when the APP is searching for wukong i8. "
? ? ? ? ? ? tmpfaq.txta = "A:The red light flashes when the APP is searching for wukong i8.The red light flashes when the APP is searching for wukong i8.The red light flashes when the APP is searching for wukong i8. "
? ? ? ? ? ? tmpF.faq = tmpfaq
? ? ? ? ? ? arrayFAQ.addObject(tmpF)
? ? ? ? }
? ? ? ? return arrayFAQ
? ? }()
?? ?
? ? private(set) var myTableView: UITableView!
? ? override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.title = "FAQ"
? ? ? ? self.initUI()
? ? }
?? ?
? ? override func didReceiveMemoryWarning() {
? ? ? ? super.didReceiveMemoryWarning()
? ? ? ? // Dispose of any resources that can be recreated.
? ? }
?? ?
? ? //MARK:- Delegate or DataSource
? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int {
? ? ? ? return 1
?? ? ? ?
? ? }
?? ?
? ? func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
? ? ? ? let tmpF : TTFaqFrame = self.faqFrames[indexPath.row] as! TTFaqFrame
? ? ? ? return tmpF.cellHeight
? ? }
?? ?
? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
? ? ? ? return self.faqFrames.count
? ? }
?? ?
? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
? ? ? ? var cell : TTFaqTableViewCell = tableView.dequeueReusableCellWithIdentifier("faq") as! TTFaqTableViewCell
? ? ? ? if cell.isEqual(nil) {
? ? ? ? ? ? cell = TTFaqTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:"faq")
? ? ? ? }
? ? ? ? cell.accessoryType = UITableViewCellAccessoryType.None
? ? ? ? cell.faqF = self.faqFrames[indexPath.row] as! TTFaqFrame
? ? ? ? return cell
? ? }
?? ?
?? ?
?? ?
? ? //MARK:- NSNotification Method
?? ?
? ? //MARK:- Action Method
?? ?
? ? //MARK:- Private Method
? ? func initUI(){
? ? ? ? //初始化Tableview
? ? ? ? myTableView = UITableView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64))
? ? ? ? myTableView.registerClass(TTFaqTableViewCell.self, forCellReuseIdentifier:"faq")
? ? ? ? myTableView.separatorStyle = UITableViewCellSeparatorStyle.None
? ? ? ? myTableView.delegate = self
? ? ? ? myTableView.dataSource = self
? ? ? ? myTableView.backgroundColor = grayColor
? ? ? ? self.view.addSubview(myTableView)
? ? }
?? ?
}
總結
以上是生活随笔為你收集整理的swift cell的高度是动态的 三个文件:控制器 cell Frame类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dumps-loads dump-lo
- 下一篇: 将字典数据写入DAT文件