Skip to content

Instantly share code, notes, and snippets.

@zackshapiro
Last active August 29, 2015 14:15
Show Gist options
  • Save zackshapiro/299382c02ac19adb8810 to your computer and use it in GitHub Desktop.
Save zackshapiro/299382c02ac19adb8810 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var items: [String] = ["We", "Aenean lacinia bibendum nulla sed consectetur.", "Vestibulum id ligula porta felis euismod semper. Maecenas sed diam eget risus varius blandit sit amet non magna. Sed posuere consectetur est at lobortis. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cras mattis consectetur purus sit amet fermentum. Etiam porta sem malesuada magna mollis euismod. Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum", "Blah", "one two three", "Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam.", "gum gum gum gum"]
@IBOutlet var tableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: TestCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TestCell
cell.labl.preferredMaxLayoutWidth = self.view.frame.width
cell.labl.text = self.items[indexPath.row]
return cell
}
// func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// println("You selected cell #\(indexPath.row)!")
// }
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell: TestCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TestCell
cell.labl.preferredMaxLayoutWidth = self.view.frame.width
cell.labl.text = self.items[indexPath.row]
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
// cell.bounds.width = tableView.bounds.width
cell.setNeedsLayout()
cell.layoutIfNeeded()
let size = cell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
let height = size.height
println(height)
// var height: CGFloat = cell.textLabel?.frame.height! as CGFloat
return height
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.estimatedRowHeight = 3.0
tableView.rowHeight = UITableViewAutomaticDimension
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool { return true }
}
# TestCell.swift
import UIKit
class TestCell: UITableViewCell {
@IBOutlet weak var labl: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment