I am a stduent and very beginner of swift3. In here, I just want to display \"First\",\"Second\",\"Third\" in the tableview and it does not look good.
The tutorial f
Try This code: Code tested in Swift 3.
Note: As you mentioned you just started programming Swift 3.I created a tableView programmatically. Just,copy below code and paste in to your viewController.run the project...
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let MyArray: NSArray = ["First","Second","Third"]
private var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Status Bar
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
// View
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
// TableView
myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
// Cell
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
// DataSource
myTableView.dataSource = self
// Delegate
myTableView.delegate = self
// View
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(MyArray[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MyArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
cell.textLabel!.text = "\(MyArray[indexPath.row])"
return cell
}
}
Output: