Database Reference
In-Depth Information
Replace the implementation of
viewDidLoad
with the following:
override
func
viewDidLoad() {
super
.
viewDidLoad
()
title
=
"\"The List\""
tableView
.
registerClass
(
UITableViewCell
.
self
,
forCellReuseIdentifier:
"Cell"
)
}
This will set a title and register the
UITableViewCell
class with the table view. You
do this so that when you dequeue a cell, the table view will return a cell of the
correct type.
Still in
ViewController.swift
, declare that
ViewController
will conform to the
UITableViewDataSource
protocol by editing the class declaration:
//Add UITableViewDataSource to class declaration
class
ViewController:
UIViewController
,
UITableViewDataSource
{
Immediately, Xcode will complain about
ViewController
not conforming to the
protocol. Below
viewDidLoad
, implement the following data source methods to fix
the error:
// MARK: UITableViewDataSource
func
tableView(tableView:
UITableView
,
numberOfRowsInSection section:
Int
) ->
Int
{
return
names
.
count
}
func
tableView(tableView:
UITableView
,
cellForRowAtIndexPath
indexPath:
NSIndexPath
) ->
UITableViewCell
{
let
cell =
tableView.
dequeueReusableCellWithIdentifier
(
"Cell"
)
as
UITableViewCell
cell.
textLabel
!.
text
=
names
[indexPath.
row
]
return
cell
}
If you've ever worked with
UITableView
, this code should look very familiar. The
first method says that the table view will have as many rows as the
names
array has
strings.