1. Edit ViewController.swift as:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView : UITableView!
//Table cell background color
let colorArray = [UIColor.lightGrayColor(), UIColor.darkGrayColor(), UIColor.yellowColor(), UIColor.cyanColor(), UIColor.purpleColor()]
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRectMake(0, view.bounds.height/2, view.bounds.width, view.bounds.height/2)
//A more correct way is to set the frame
//as CGRectMake(0, view.bounds.height/2,
//view.bounds.height/2, view.bounds.width) here,
//i.e. swap the width/height values
//before rotation.
//A more correct way is to set the frame
//as CGRectMake(0, view.bounds.height/2,
//view.bounds.height/2, view.bounds.width) here,
//i.e. swap the width/height values
//before rotation.
tableView = UITableView(frame: frame)
tableView.delegate = self
tableView.dataSource = self
//Remove gaps at margins #1
if tableView.respondsToSelector(Selector("separatorInset")) {
tableView.separatorInset = UIEdgeInsetsZero;
}
if tableView.respondsToSelector(Selector("layoutMargins")) {
tableView.layoutMargins = UIEdgeInsetsZero;
}
tableView.transform = CGAffineTransformMakeRotation(-CGFloat(M_PI_2))
//Set the frame size again after rotation.
tableView.frame = frame
view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myIdentifier")
cell.textLabel?.text = "Cell #\(indexPath.row)"
cell.detailTextLabel?.text = "Subtitle"
cell.backgroundColor = colorArray[indexPath.row]
cell.contentView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
//Remove gaps at margins #2
if cell.respondsToSelector(Selector("separatorInset")) {
cell.separatorInset = UIEdgeInsetsZero;
}
if cell.respondsToSelector(Selector("preservesSuperviewLayoutMargins")) {
cell.preservesSuperviewLayoutMargins = false;
}
if cell.respondsToSelector(Selector("layoutMargins")) {
cell.layoutMargins = UIEdgeInsetsZero;
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
return view.bounds.width/3
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2. Run the iOS simulator to get the result: