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)!")
    }


}
@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