Created
June 2, 2014 20:58
-
-
Save jquave/8ca03aad33490a2ffa73 to your computer and use it in GitHub Desktop.
Example code for Table View in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
//
// 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
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