Skip to content

Instantly share code, notes, and snippets.

@phynet
Last active November 14, 2023 12:43
Show Gist options
  • Save phynet/075d538c93c0bfe62596bc4d47a482e1 to your computer and use it in GitHub Desktop.
Save phynet/075d538c93c0bfe62596bc4d47a482e1 to your computer and use it in GitHub Desktop.
Template to create a simple UITableView in a UIViewController
  1. Add UItableView inside ViewController in Storyboard.
  2. Create IBOutlet called tableView
  3. Set Delegate and DataSource in ViewController in Storyboard
class ViewControllerCustom: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
     var items: [String] = ["Swift1", "Swift2", "Swift3"]

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
        
        cell.textLabel?.text = self.items[indexPath.row]
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }


}
@fmaxx
Copy link

fmaxx commented Sep 21, 2018

Thanx!

@superarts
Copy link

superarts commented Jul 14, 2021

2021 Swift 5 version:

protocol Example {
    func run()
    init()
}

class ViewController: UIViewController {
    @IBOutlet var tableView: UITableView!

    let examples: [Example.Type] = [
        Example1.self,
        Example2.self,
    ]

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

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ExampleCell")! as UITableViewCell
        cell.textLabel?.text = "\(examples[indexPath.row])"
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return examples.count
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        examples[indexPath.row].init().run()
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

@evolventum
Copy link

evolventum commented Nov 12, 2023

is there forgetter some code in viewDidLoad() ?

tableView.delegate = self
tableView.dataSource = self

@superarts
Copy link

is there forgetter some code in viewDidLoad() ?

tableView.delegate = self
tableView.dataSource = self

This gist was written 7 years ago, people were using interface builder back then and IBOutlets were dragged between components in IB.

@phynet
Copy link
Author

phynet commented Nov 14, 2023

Correct, this gist was aimed to be used with a StoryBoard or IBOutlets in general. Also the third step specify to set it on the Storyboard. Uff oldddd times xD

  1. Set Delegate and DataSource in ViewController in Storyboard

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