Reputation: 1334
I am trying to create a UITableView
in Swift. I followed a tutorial, but the table
doesn't have the values that I try putting in it in the code. Here is the code:
class settingsVC2: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
Upvotes: 0
Views: 978
Reputation: 1288
You have to connect the table (delegate & datasource) to the ViewController itself.
Or you can do it programmatically by adding the following lines to your viewDidLoad()
:
self.tableView.dataSource = self
self.tableView.delegate = self
Upvotes: 5