Started practice swift. In singleViewController I am trying to make a UITableView
. In storyboard I set the datasource and delegate. Here I am getting the error
This is a common warning which means that " you haven't implemented the required methods of the protocol yet"
A view object on the storyboard may need a datasource. For example, TableView needs a datasource and usually, View Controller acts as one.
So Table View expects the ViewController to contain the methods which return the 'must have' information for the table view.
Table view needs to know the number of sections, number of rows in each section etc..
Unless all the required information is returned by the datasource object, the warning will persist.
You need to implement all the required methods of UITableViewDataSource
in order to get rid of that error.
Basically... you're missing:
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
//return XX
}
The answer of Ankit worked for me in Xcode 8 for Swift 2.3. Here’s the new syntax.
extension ViewController: UITableViewDataSource {
internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return 1
}
internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//return cell
}
}
You should implement all the required methods before the last }
, but you have written them outside of the UIViewController. Also, you need to change the func for number of lines.
the suggested edit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int
{
return 20
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
}
Just a suggestion to improve readability, you can separate your protocols using extension, like this:
class ViewController: UIViewController {
// Your lifecycle code here
}
extension ViewController: UITableDataSource {
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 20
}
}
extension ViewController: UITableViewDelegate {
...
}
with some edits suggested by auto complete, from the above answer by @MB_iOSDeveloper on swift 3, Xcode 8.3.2, this code works for me:
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//currently only a testing number
return 25
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "mycell")
cell.textLabel?.text = "row#\(indexPath.row)"
cell.detailTextLabel?.text = "subtitle#\(indexPath.row)"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}