XCODE: Doesn't conform to UITableViewDataSource - methods are right, {} are right - ios

Please help, I'm at my wit's end. I've followed multiple tutorials, read every thread similar to this I can find, and five hours later, I'm still lost and getting the same error: "type ViewController doesn't conform to UITableViewDataSource."
Code:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var proTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
proTable.reloadData()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return pcMgr.pros.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Default Pros")
cell.textLabel?.text = pcMgr.pros[indexPath.row].name
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.delete){
pcMgr.pros.remove(at: indexPath.row)
proTable.reloadData()
}
}
}
}

Replace your code with this , you have used old syntax that to inside viewDidLoad function.
import UIKit
class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var proTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
proTable.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Default Pros")
cell.textLabel?.text = pcMgr.pros[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pcMgr.pros.count
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.delete){
pcMgr.pros.remove(at: indexPath.row)
proTable.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

The syntax for the required UITableViewDataSource mehods are not correct.
Try this:
#available(iOS 2.0, *)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
<#code#>
}
#available(iOS 2.0, *)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
<#code#>
}
Because you have already conformed to the protocol, I would recommend using XCode's auto complete on method declarations. Use escape key to see the method declarations.

The correct syntax for the protocol functions in Swift 3
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell ;
return cell
}
Use these methods outside of viewDidLoad method. These methods should be inside your viewDidLoad.

-Remove the functions from viewDidLoad()
-Mention the array
-Add the function "numberOfSections" to the code
hope this helps!
import UIKit
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
#IBOutlet var proTable: UITableView!
var pcMgr: Array = [] // Or whatever your array is!
override func viewDidLoad() {
super.viewDidLoad()
proTable.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return pcMgr.pros.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Default Pros")
cell.textLabel?.text = pcMgr.pros[indexPath.row].name
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.delete){
pcMgr.pros.remove(at: indexPath.row)
proTable.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Code Optimisation in terms of (UI) Delegate methods in Swift

I am asked to do optimise my swift code especially when it comes to UI Delegate methods.
Scenario: I have UITableView in five UIViewControllers, in each view controller I am using UITableView delegate methods due to which my code is repeating. So how can I optimise this thing?
Below is the structure of code: You can see delegate method code is repeating
class FirstViewController: UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var userArray = [UserModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
// register cell according to model (Dynamic Cells)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell01", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
class SecondViewController: UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var productArray = [ProductModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
// register cell according to model (Dynamic Cells)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell01", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}

Compile & Build error related to "Type 'ViewController' does not conform to protocol 'UITableViewDataSource'"

I am not understanding why my app is not compiling. This is the output currently:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var IndexArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
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 IndexArray.count
}
func tableView(tableView: UITableView, tiltleForHeaderInSection section: Int) -> String? {
return IndexArray[section]
}
func sectionIndexTitlesfortableView (tableView: UITableView) -> [String]? {
return IndexArray
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath as IndexPath) as! TableCell
cell.imgPhoto.image = UIImage(named: "charity")
cell.lblUserName.text! = "User Name"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
You are missing to specify a few methods declared in the protocols your class is deriving from.
func tableView(UITableView, cellForRowAt: IndexPath)
Required. Asks the data source for a cell to insert in a particular location of the table view.
func tableView(UITableView, numberOfRowsInSection: Int)
Required. Tells the data source to return the number of rows in a given section of a table view.
At least the two methods above must be declared in your class, otherwise you get the error.
These are just the required methods, but for functioning in the correct way you need to define others. See Apple documentation on UITableViewDataSource protocol
In Swift 3 all method signatures have been changed to:
func numberOfSections(in tableView: UITableView) -> Int { }
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { }
func sectionIndexTitles(for tableView: UITableView) -> [String]? { }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}

swift 3 error: does not conform to protocol ‘UITableViewDataSource’

using custom swift class (not main ViewController)
Code:
import Foundation
class Myclass: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView!
var items: [String] = ["Item1", "Item2", "Item3"]
override func viewDidLoad() {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "td")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "td")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
print("You selected cell #\(indexPath.row)!")
}
}
The correct syntax for the protocol functions in Swift 3 (resolves error):
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")
cell.textLabel?.text = items[indexPath.row]
return cell
}
Your method doesn't match the protocol. In swift 3 the protocol methods are different. Verify that they match.

Type "someViewContoller" does not conform to protocol "UITablViewDataSource"

I am really new to the whole Swift thing and also programming in general. I have researched everywhere but couldn't find an answer for the error that I got. Please help me :)
import Foundation
import UIKit
class TümÜrünlerViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tümÜrünlerTableView: UITableView!
#IBAction func cancelTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
var ürünler = ["Havalı", "Daha Havalı", "Eheheh"]
var ürün = "Balık"
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ürün = self.ürünler[indexPath.row]
self.performSegueWithIdentifier("başlıklardanÖzeleSegue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tümÜrünlerTableView.dataSource = self
self.tümÜrünlerTableView.delegate = self
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailViewController = segue.destinationViewController as BaşlıklarViewController
detailViewController.ürün = self.ürün
if self.ürün == "Havalı" {
detailViewController.label = "anan"
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
return self.ürünler.count
}
}
Here is my code but I am getting the error when I try to insert UITableViewDataSource. I tried everything that the forums said but it docent seem to work for me :(
My guess:
Let us look at this method. T needs to be decapitalized. tableView not TableView
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
It's a very simple mistake - the UITableViewDataSource requires that 2 methods are implemented:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
but you've just made a typo in the 2nd one:
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
^
the method name should start in lowercase: tableView(...)
You have a capital T on your cellForRowAtIndexPath function. It should read:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
Reason behind error is that, you are not calling datasource required method in correct order. Correct order of calling is :
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Always, make sure you call numberOfRowsInSection before cellForRowAtIndexPath.

ViewController not conforming to UITableViewDataSource? [duplicate]

Here is my Code , Compiler is still showing error although I wrote implementation of both methods
Please tell why
import UIKit
class FirstViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {
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 taskMgr.tasks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
Here is screenshot of Xcode
You have probably written this code in a previous version of Xcode. The correct signature for the 2 methods has changed, and it's now:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
The tableView parameter is no longer an optional.

Resources