Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

Practice began quickly. In singleViewController, I am trying to make a UITableView . In the storyboard, I set the data source and the delegate. Here I get the error message * 'ViewController' does not match the protocol 'UITableViewDataSource' *

Screenshot for error

 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 numberOfSectionsInTableView(tableView: UITableView!) -> 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 } 
+44
ios uitableview swift
Aug 30 '14 at 11:19
source share
17 answers

You must implement all the necessary methods to the last } , but you wrote them outside of the UIViewController. In addition, you need to change func for the number of lines.

proposed editing

 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 } } 
+66
Aug 30 '14 at 11:36
source share

Try to delete! according to your function. It helped me

 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 } 
+15
Oct 22 '14 at 11:26
source share

You need to implement all the necessary methods of UITableViewDataSource to get rid of this error.

Basically ... you are missing:

 func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { //return XX } 
+9
Aug 30 '14 at 11:23
source share

I had the same problem: everything would be easy to work in Objective-C, perhaps because we are currently more familiar with this, but in this case the fast is very new, so its error notifications are pretty vague.

While I was running a UITableView based application, and I ran into this problem. I opened the implementation file for UITableView by clicking a command and clicking on UITableView. In the implementation file, we can clearly see that two functions are required for implementation,

  • func tableView (tableView: UITableView, numberOfRowsInSection: Int) -> Int
  • func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

I came to this post and started collecting things while retaining meager knowledge of objective-C programming. The reason for the error is that the table view is determined by two elements, first a section and rows in a section, as well as tableview cells. By default, there is at least one section in the table view, but we need to match the number of rows in the section. Secondly, we need to know which cell we are going to represent in a certain line in the section. Despite this, even if we use the UITableViewCell by default, we still need an identifier to access it in order to set its subviews or properties. I hope this was useful, A bit of soft criticism will be appreciated, as I myself am very new to Swift :)

+4
Nov 02 '14 at 8:46
source share

I just ran into the same issue in Swift.

You must implement all the functions for the UITableViewDataSource in the class, which means that the following functions must be implemented:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int{} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} 

I am not sure if the function works for the numberOfSectionsInTableView function or not. For me, I have to realize this in my class.

+2
Apr 09 '15 at 6:29
source share

The following code did not work for me on iOS 8.1. in Xcode 6.1.1. This code works:

 import UIKit class ViewController : UIViewController,UITableViewDelegate,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ //currently only a testing number return 25 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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. } } 
+1
Jan 16 '15 at 9:40
source share

Just add these two methods, then this error will be resolved [XCode8 Swift3]

first method:

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } 

Second method:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } 
+1
Dec 07 '15 at 5:03
source share

Pull all options for tableview and NSIndexpath for latest version of Xcode 6.1.1 GM_Seed

0
Nov 18 '14 at 21:36
source share

Just to improve readability, you can separate your protocols with an extension, for example:

 class ViewController: UIViewController { // Your lifecycle code here } extension ViewController: UITableDataSource { func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { return 20 } } extension ViewController: UITableViewDelegate { ... } 
0
Jan 16 '15 at 9:56
source share

This is probably due to a typo or incorrect style of the method name. I used cmd + left-click on the UITableView and copied and pasted the method names to my UIViewController ; which will not work on Swift .

Instead, enter func tableView , find the desired method in the list, and let autocomplete do its job.

Autofill

0
Nov 24 '15 at 16:44
source share

This is a general warning, which means that "you have not yet completed the required protocol methods"

The storyboard view object may require a data source. For example, a TableView needs a data source and usually, the View Controller acts as one.

So, Table View expects the ViewController to contain methods that return "must have" information to represent the table.

The table view should indicate the number of sections, the number of rows in each section, etc.

If all the necessary information is returned to the data source object, the warning will be saved.

0
Mar 25 '16 at 13:03
source share

Change the syntax of the required protocol methods "UITableViewDataSource" according to the new operation of quick document 3:

internal func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

internal func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

This worked for me when compiling with a fast 3 compiler

0
Sep 06 '16 at 7:43
source share

Answer Ankit worked for me in Xcode 8 for Swift 2.3. Here is 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 } } 
0
Sep 16 '16 at 8:06
source share

Add these methods

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } 
0
Jan 24 '17 at 11:40
source share

Copy the code below under the ViewController class and specify the number of rows you want (in the first section) and define the contents of each cell (in the 2nd function)

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 //no. of rows in the section } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: <#T##UITableViewCellStyle#>, reuseIdentifier: <#T##String?#>) } 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. } 

}

0
Feb 13 '17 at 15:42
source share

with some corrections suggested by autocompletion from the above answer from @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. } 

}

0
Apr 23 '17 at 7:34 on
source share

In general, a protocol has optional and required methods. For example, UISearchBarDelegate and UITableViewDelegate are those cases where you can declare compliance with the protocol without using any of their methods. But this does not work fine for a UITableViewDataSource.

In the official documentation, the protocol is "UITableViewDataSource" → Symbols → Table view settings, methods: func tableView(UITableView, cellForRowAt: IndexPath) and func tableView(UITableView, numberOfRowsInSection: Int) in bold Required .

0
Jun 27 '17 at 12:32
source share



All Articles