Skip to content

Instantly share code, notes, and snippets.

@DamienBell
Last active May 31, 2016 17:00
Show Gist options
  • Save DamienBell/915ef623f3da3a6525a73c3453c85159 to your computer and use it in GitHub Desktop.
Save DamienBell/915ef623f3da3a6525a73c3453c85159 to your computer and use it in GitHub Desktop.
These are some of the basics of UITableView implementation that are used repeatedly. Logic is added here to account for dynamically sizing TableCells, as well as loading a tableCellView from a nib.
//
// ShopsDetailsTableViewController.swift
// Only Indie Coffee
//
// Created by Damien Bell on 5/31/16.
// Copyright © 2016 Damien Bell. All rights reserved.
//
import UIKit
class CustomTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
/* */
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 126.0
tableView.delegate = self
tableView.dataSource = self
//Prevents the table from using filler cells to extend to the bottom of the table
tableView.tableFooterView = UIView(frame: CGRectZero)
/*
Don't forget to set your Table Cell Row Height to a custom value in you CustomTableCell.xib.
It's also nessesary to set the identifier.
*/
tableView.registerNib(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "CustomTableCell")
}
// MARK: UITableViewDelegate
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 126.0
}
// MARK: UITableViewDataSource
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{
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as! CustomTableCell
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment