How to create table view in swift 3.0 Pragmatically
import UIKit
var studentTable:UITableView = UITableView();
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let colorArray = ["Red", "Blue", "Green"];
let screenWidth = UIScreen.main.bounds.width;
let screenHeight = UIScreen.main.bounds.height;
override func viewDidLoad() {
super.viewDidLoad()
studentTable = UITableView.init(frame: CGRect(x: 0, y: 50, width: screenWidth, height: screenHeight));
studentTable.delegate = self;
studentTable.dataSource = self;
studentTable.backgroundColor = UIColor.white;
self.view.addSubview(studentTable);
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return colorArray.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
let colorLable = UILabel.init(frame: CGRect(x: 10, y: 10, width: screenWidth-20, height: 30));
colorLable.backgroundColor = UIColor.lightGray;
colorLable.text = colorArray [indexPath.row];
if indexPath.row == 0
{
colorLable.textColor = UIColor.red;
}
else if indexPath.row == 1
{
colorLable.textColor = UIColor.blue;
}
else
{
colorLable.textColor = UIColor.green;
}
colorLable.textAlignment = NSTextAlignment.center;
cell.contentView.addSubview(colorLable);
return cell;
}
}