Adding a Table View in swift is same as Objective-C.
Create a new Single View Application and give any name you want, make sure you select 'Swift' language.
Go to storyboard and add UITableView, make a IBOutlet for Table view
Now, switch to ViewController .swift class and add UITableViewDelegate and UITableDataSource
Class RolePopOverViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{
// IBOutel for UITableView
@IBOutlet weak var roleTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Note: You can see a error "Type ViewController does not conform to Protocol ''UITableViewDataSource' ". Its just because we didn't add two required Table View Data Source Method yet.
create a NSArray instance globally
var allRoleDataArray : NSArray!
Now register UITableViewCell class
roleTableView .registerClass(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
//initialize globally declare array
allRoleDataArray = ["One","Two","Three","Four","Five"]
Add above lines in viewDidLoad()
Now time to add UITableView Data Source method
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allRoleDataArray.cout
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
var value : NSString = allRoleDataArray[indexPath.row] as NSString
cell.textLabel?.text = value
return cell;
}
you can't see error any more.
Build and run the app.
Now open ViewController.swift and add ''didSelectRow'' method
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var value : NSString = allRoleDataArray[indexPath.row] as NSString
tableView.deselectRowAtIndexPath(indexPath, animated: true)
NSLog("Selected Row = %@", value)
}
Again run the App and select any row you can see some message on console.
Ex: if you select second row you can see "Selected Row = Two"
Create a new Single View Application and give any name you want, make sure you select 'Swift' language.
Go to storyboard and add UITableView, make a IBOutlet for Table view
Now, switch to ViewController .swift class and add UITableViewDelegate and UITableDataSource
Class RolePopOverViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{
// IBOutel for UITableView
@IBOutlet weak var roleTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Note: You can see a error "Type ViewController does not conform to Protocol ''UITableViewDataSource' ". Its just because we didn't add two required Table View Data Source Method yet.
create a NSArray instance globally
var allRoleDataArray : NSArray!
Now register UITableViewCell class
roleTableView .registerClass(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
//initialize globally declare array
allRoleDataArray = ["One","Two","Three","Four","Five"]
Add above lines in viewDidLoad()
Now time to add UITableView Data Source method
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allRoleDataArray.cout
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
var value : NSString = allRoleDataArray[indexPath.row] as NSString
cell.textLabel?.text = value
return cell;
}
you can't see error any more.
Build and run the app.
Now open ViewController.swift and add ''didSelectRow'' method
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var value : NSString = allRoleDataArray[indexPath.row] as NSString
tableView.deselectRowAtIndexPath(indexPath, animated: true)
NSLog("Selected Row = %@", value)
}
Again run the App and select any row you can see some message on console.
Ex: if you select second row you can see "Selected Row = Two"