Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for Table View in Swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
cell.text = "Row #\(indexPath.row)"
cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"
return cell
}
}
@ccorcos
Copy link

ccorcos commented Jul 29, 2014

It doesn't make sense that we don't get this boilerplate code anymore...

@garretthunyadi
Copy link

garretthunyadi commented Aug 1, 2014

cell.text is no longer kosher. cell.textLabel.text is what all the cool kiddies are doing these days.

@GeoffreyHinck
Copy link

GeoffreyHinck commented Aug 13, 2014

How is this able to work without: @IBOutlet weak var (appname): UITableView! - in the viewcontroller?

@OscarApeland
Copy link

OscarApeland commented Aug 24, 2014

@ccorcos it's a beta, it makes total sense.

@shawnhansen
Copy link

shawnhansen commented Sep 11, 2014

I had to alter the code to the following to eliminate errors and build.

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

        cell.textLabel?.text = "Row #\(indexPath.row)"
        cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"

        return cell
    }

}

@krishnaff
Copy link

krishnaff commented Sep 28, 2014

what @GeoffreyHinck mentioned already:
How is this able to work without: @IBOutlet weak var (appname): UITableView! - in the viewcontroller?

Seriously, isn't IBOutlet line not required?

@beerbubble
Copy link

beerbubble commented Sep 29, 2014

@insaneinc01 IBOutlet line is required. if delete this line , i got a NSUnknownKeyException

@jpotts18
Copy link

jpotts18 commented Oct 8, 2014

This is working for me

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "TestCell")

        cell.textLabel?.text = "Row #\(indexPath.row)"
        cell.detailTextLabel?.text = "Subtitle  #\(indexPath.row)"

        return cell;
    }

}


@novitskiy-aleksei
Copy link

novitskiy-aleksei commented Oct 21, 2014

@geekles thx! It works for me

@lunks
Copy link

lunks commented Dec 3, 2014

This worked for me just fine:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "MyTestCell")

        cell.textLabel?.text = "Row #\(indexPath.row)"
        cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Note I don't have any @IBOutlet declarations and style can just be passed as .Subtitle.

@wikibootup
Copy link

wikibootup commented Jun 10, 2015

As a Swift beginner, without dataSoure of IBOutlet
I couldn't implement table view in view controller
So I added dataSoure of IBOutlet

Here's link
https://github.com/my-snippet/iOS-Swift-Snippet/blob/iOS-App-01/TableViewInViewController/TableViewInViewController/ViewController.swift

@IBOutlet weak var table: UITableView!
yourTableOutlet.dataSource = self
override func viewDidLoad() {
        super.viewDidLoad()
        ...
        yourTableOutlet.dataSource = self
        ...
    }

@randikac32
Copy link

randikac32 commented Sep 17, 2015

//
// ViewController.swift
// HelloSwift
//
// Created by Randika Chandrapala on 9/17/15.
// Copyright (c) 2015 Kasun Randika. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // 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.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "MyTestCell")

    cell.textLabel?.text = "Row #\(indexPath.row)"
    cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"

    return cell
}

}

Above worked for me too. At first I put a IBOutlet just for trying it. then after I read the thread, I deleted it and tried, it still works. I think previous XCode 6 Betas might needed an IBOutlet. only thing I've connected is UITableViewDataSource and UITableViewDelegate.

Note: My XCode version is Version 6.3.2 (6D2105) running on Mac OS X Yosemite (10.10.3 (14D136))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment