Ответ 1
Вы добавляете новую переменную экземпляра табличного представления ниже объявления класса.
@IBOutlet weak var tableView: UITableView!
Чтобы соответствовать протоколам UITableViewDelegate
и UITableViewDataSource
, просто добавьте их через запятую после UIViewController
в объявлении класса
После этого нам нужно реализовать tableView(_:numberOfRowsInSection:)
, tableView(_:cellForRowAtIndexPath:)
и tableView(_:didSelectRowAtIndexPath:)
в классе ViewController
и оставить их пока пустыми
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0 // your number of cells here
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// your cell coding
return UITableViewCell()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// cell selected code here
}
}
Как упомянуто @ErikP в комментариях, вам также нужно установить self.tableView.delgate = self
и self.tableView.dataSource = self
в методе viewDidLoad
(или в Storyboard тоже работает хорошо).