Create a table with custom table view cell

Create a table

  1. Drag object "Table View" from Object Library on View Controller
  2. Open swift file for this View Controller
  3. Highlighting UITableView object on View Controller clicked for it
  4. Hold down control button drag directrix in swift file inside class for this view controller
  5. Write name for Table View inside pop-up window
  6. In swift file will be created outlet for tableView
    @IBOutlet var myTableView: UITableView!
  7. Add UITableViewDataSource and UITableViewDelegate protocols to UIViewController
  8. You'll get error "Type 'YourViewController' does not conform to protocol 'UITableViewDataSource"
  9. Next, create 3 functions tableView
    Start typing func tableView and choose from dropdown.
    • numberOfRowsInSection
    • cellForRowAtIndexPath
    • didSelectRowAtIndexPath
  10. Edit numberOfRowsInSection function
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
    }
  11. Add deletage and datasource in viewDidLoad after func super.viewDidLoad()
    myTableView.delegate = self
    myTableView.dataSource = self

Create custom table view cell

  1. Create a new .xib file (⌘ + N ➝ iOS ➝ Empty)
  2. Add Table View Cell from Object Library to your xib
  3. Create a class for custom view cell in new swift file or swift file View Controller, where created Table View
    class CustomTableViewCell: UITableViewCell {}
  4. Assign created class to Table View Cell in properties (Identity inspector ➝ Class)
  5. Create nib in ViewDidLoad
    let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
    myTableView.registerNib(nib, forCellReuseIdentifier: "customCell")
    Where CustomTableViewCell is the class of custom table view cell and customCell is any identifier.
  6. Edit cellForRowAtIndexPath
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = myTableView.dequeueReusableCellWithIdentifier("customCell") as! CustomTableViewCell
    return cell
    }
    Where CustomTableViewCell is the class of custom table view cell and customCell is identifier from previous step.