开发者社区> 问答> 正文

关于IOS SWIFT UIControllerView 里的tableviewcell自适应高度

想实现让cell 根据label里的数据自动调整高度让label多行显示数据,根据网上给的自适应方法没有实现,还希望伙伴们给点指导,下面附上代码和截图

 class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat{
 // return 44
 // }
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell : TableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell

    if(cell == nil)
    {
        cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)[0] as! TableViewCell;
    }
    cell.first.text = "nnfsfhsdsdsdifsfsfsdfisfsidfhsdfhdhfsdfsdfhdfhhfhsdfhsdhfshdhsdfsdd"
    cell.second.text = "rirgjf"
    cell.frame.size.height = cell.first.frame.size.height
    return cell as UITableViewCell
}


}
 import UIKit

class TableViewCell: UITableViewCell {
@IBOutlet weak var first: UILabel!
@IBOutlet weak var second: UILabel!
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
}


}

screenshot

展开
收起
爵霸 2016-03-09 15:58:37 2844 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载