Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created December 25, 2014 07:21
Show Gist options
  • Save cemolcay/6c4ce66f154e3c6bb996 to your computer and use it in GitHub Desktop.
Save cemolcay/6c4ce66f154e3c6bb996 to your computer and use it in GitHub Desktop.
iOS 8 AutoSized Table View in Swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var dataSource: [String] = []
var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...10 {
var s = "adsda"
for _ in 0...arc4random_uniform(40) {
s += "asd"
}
dataSource.append(s)
}
table = UITableView (frame: view.frame)
table.dataSource = self
table.delegate = self
table.estimatedRowHeight = 60
table.rowHeight = UITableViewAutomaticDimension
table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(table)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = table.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = dataSource[indexPath.row]
cell.textLabel?.numberOfLines = 0
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment