说明:  在控制器中 申明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类相关推荐

  1. java文件读写的基本类_java常用工具类(三)—— 文件读取的操作类

    定义常用的文件类型 public class FileType { /** * 文件头类型 */ public static final String XML_FILE = "text/xm ...

  2. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件: shopCell.h #import <UIKit/UIKit.h> @interface shopCell : UITableViewCe ...

  3. 用Swift实现一款天气预报APP(三)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 通过前面的学习,一个天气预报的APP已经基本可用了.至少 ...

  4. 通过代码自定义cell(cell的高度不一致)

    我们知道,在iOS中,自定义cell的方式有两种: 一是通过xib创建 .二是通过代码自定义cell 这里我说下通过代码自定义的cell. 当我们的应用显示的cell比较复杂,显示的行高都不一样,比如 ...

  5. 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局...

    文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...

  6. SDAutoLayout快速实现Cell的高度自适应

    我们经常会遇到需要cell高度自适应的情况 SDAutoLayout可以帮助你快速的实现这个功能 第一步 cell里面自定义 在这里只显示 姓名 电话 地址 三个控件 -(id)initWithSty ...

  7. iOS聊天室 简单的对话聊天界面(cell自适应高度)

    文章目录 难点 思路 需要用到的方法的大致解析(只是简单的介绍,如果想要仔细理解推荐再去看看别的博客) GitHub地址 代码 效果图 难点 因为聊天长度不一样,需要设置自适应高度 发送信息后,需要使 ...

  8. iOS8新特性 计算 cell 的高度

    http://tutuge.me/2015/08/08/autolayout-example-with-masonry2/ 1.tableview: 自动计算 tableVIew 的 cell 的高度 ...

  9. (0072)iOS开发之UITableViewCell高度自适应探索--cell预估高度

    转载自:http://www.jianshu.com/p/f3609cd9392e 有了预估高度这个先决条件,一切都好说了.我们直接从代码入手. 接下来我们实现一个简单的信息展示功能,如: Demo最 ...

最新文章

  1. C语言 用typedef定义类型
  2. 如何根据原理图画封装_常用原理图封装
  3. java类的实例参数传递_获取我正在通过参数传递的相同Java类实例
  4. java checker_java 英文单词纠正校验框架(Word Checker)
  5. 多线程 thread java_java Thread 多线程
  6. python的返回函数的作用_函数的返回值和作用域
  7. mac perl dbd mysql_Install DBD::mysql for Perl in XAMPP in Mac , solving errors
  8. 5.Django 数据库多表查询
  9. maven安装oracle驱动,maven 安装 Oracle 驱动 ojdbc14.jar
  10. C++设计模式 - 适配器模式(Adapter)
  11. 恒德可视化指挥调度解决方案
  12. 《软件架构/架构师书库》读后感
  13. 数据库概论之无损分解
  14. ASP.NET学习(一)
  15. R语言apply族函数详解
  16. 用evo工具箱评估LeGO-LOAM轨迹
  17. InnoDB: Assertion failure in thread 140536591259392 in file page0zip.ic
  18. BM33-二叉树的镜像
  19. Apache的管理及优化——中篇{Apache的访问控制(黑白名单,用户密码认证)、Apache的虚拟主机}
  20. LabVIEW编程LabVIEW开发 研华PCIE-1751更改DIO方向 例程与相关资料

热门文章

  1. 一篇文章带你了解-selenium工作原理详解
  2. php通过身份证号码查找身份证归属地信息
  3. 【数学建模】图论模型(基础理论+最大流与最小费用流问题)
  4. java实现自动组卷要用什么算法_基于Java的自动组卷系统的实现
  5. 微博短链接的生成算法(Java版本)
  6. 中科蓝讯--修改蓝牙名字的方法
  7. 快速找到 Linux Kernel 中各种函数原型的方法
  8. Python3处理HTTPS请求 SSL证书验证
  9. 背景图片全屏适应的两种方法,background-size: cover; 或者(background-size: 100% 100%;)
  10. 【已知线电压且零序电压为零,三相电压幅值】