import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let myiPhoneItems: NSArray = ["iOS9","iOS8", "iOS7", "iOS6", "iOS5", "iOS4"]
private let myAndroidItems: NSArray = ["5.x", "4.x", "2.x", "1.x"]
private let mySections: NSArray = ["iPhone", "Android"]
override func viewDidLoad() {
super.viewDidLoad()
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSections(in tableView: UITableView) -> Int {
return mySections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mySections[section] as? String
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
print("Value: \(myiPhoneItems[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(myAndroidItems[indexPath.row])")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return myiPhoneItems.count
} else if section == 1 {
return myAndroidItems.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = "\(myiPhoneItems[indexPath.row])"
} else if indexPath.section == 1 {
cell.textLabel?.text = "\(myAndroidItems[indexPath.row])"
}
return cell
}
}
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let myiPhoneItems: NSArray = ["iOS9","iOS8", "iOS7", "iOS6", "iOS5", "iOS4"]
private let myAndroidItems: NSArray = ["5.x", "4.x", "2.x", "1.x"]
private let mySections: NSArray = ["iPhone", "Android"]
override func viewDidLoad() {
super.viewDidLoad()
let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mySections.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return mySections[section] as? String
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
print("Value: \(myiPhoneItems[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(myAndroidItems[indexPath.row])")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return myiPhoneItems.count
} else if section == 1 {
return myAndroidItems.count
} else {
return 0
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = "\(myiPhoneItems[indexPath.row])"
} else if indexPath.section == 1 {
cell.textLabel?.text = "\(myAndroidItems[indexPath.row])"
}
return cell
}
}