func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("WTF is this getting hit...")
let Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
Cell.textLabel?.text = self.funlists[indexPath.row]
print("This better be getting hit")
return Cell;
}
for some reason this method isn't getting called.
i have set the following
uiTableView.delegate=self
uiTableView.dataSource=self
uiTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
and I have also included,
class viewUniversityList: UIViewController, UITableViewDelegate, UITableViewDataSource {
There might be several reason for this. Some of them are :
If you are using tableView in your ViewController file, then you should add the UITableViewDelegate and UITableViewDelegate delegates, like this :
class ViewController: UIViewController,UITableViewDelegate, UITableViewDelegate { ... }
If you have created a tableView in code then you should do the
following :
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
If that's all done, or you have create UITableView through storyboard with a separate class which is a subClass of UITableView, then you definitely need to define the
following methods :
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {...}
~ func numberOfSectionsInTableView(tableView: UITableView) -> Int {...} is optional as suggested by #rmaddy, but it's a good practice to define it.
delegate property is not set on the tableView?
also tableView:numberOfRowsInSection: should be implemented
you have to set your delegates and declare that you use those protocols
in file.m in didLoad
_table.delegate = self;
_table.dataSource = self;
in file.h when you declare the interface you have to add these protocols UITableViewDelegate and UITableViewDataSource
Xcode will tell you which methods you must implement to respect the protocol.
Related
Here are the relevant sections of code:
class SeriesViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> CustomTVCell {
And yet, Xcode insists that:
Type 'SeriesViewController' does not conform to protocol 'UITableViewDataSource'
And politely asks:
Do you want to add protocol stubs?
I tried doing that, then moved all the code from the existing delegate K, but it still says it doesn't conform.
It apparently recognizes my numberOfRowsInSection, because it doesn't complain about that.
This happened after I changed SeriesViewController from a UITableviewController to a UIViewController and added the tableview and cell manually.
Can anybody help?
TIA!
I'm playing around with the UITableView in XCode 10.2.1 with Swift 5. According to the Apple Developer docs, adopting the UITableViewDataSource is the most straightforward way to populate a UITableView with dynamic data.
So I copied the necessary methods to override into a custom class:
import Foundation
import UIKit
class MyDataSource : NSObject, UITableViewDataSource {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Fetch a cell of the appropriate type.
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
// Configure the cell’s contents.
cell.textLabel!.text = "Cell text"
return cell
}
}
But my code won't compile. All I get is a "Method does not override any method from its superclass" error. What? I even did used the autocomplete feature from XCode and it generated the stubs for me, yet I still cannot build my project. What is the solution?
Your class' superclass is NSObject which does not have those table view data source methods to override in a subclass. I believe that is what that compiler is saying.
I think if you take off the override keyword on those function declarations that could help.
I have a ViewController that calls (clicking on a button) another View using this function
#IBAction func btnSeeContact(sender: AnyObject) {
self.performSegueWithIdentifier("segueSeeContact", sender: self)
}
and my prototype cell is "linked" to a custom View Controller named ContactsTableViewCell that I have created and it implements:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell
cell.txtName.text = "test"
cell.txtPhone.text = "1234567890"
return cell
}
When I run the project, the button calls the table, but there is no Cell on it, and I put a breakpoint on those tableView functions and they are not being reached.
What am I missing here that those functions are never being called?
I am adding a new answer since my previous answer was up voted, so I don't want to make massive edits that one, and is still a valid way to fix your issue.
The issue is you have your custom classes confused. In your screen shot you can see that the the Table View Controller is not set to a custom class, it just says Table View Controller. That is the object that needs to get a custom implementation of the UITableViewController class.
Instead you seem to be setting the cell's class to a custom class, and implementing the delegate methods there. You still need a custom class for the table view cell, but it should be a custom class of UITableViewCell.
So your cell class should look something like this:
import UIKit
class YourCustomTableViewCell: UITableViewCell {
#IBOutlet weak var yourLabel1: UILabel!
#IBOutlet weak var yourLabel2: UILabel!
}
You will be given an instance of this cell to configure in cellForIndexPath.
So your Table view controller class should be set to a class that looks like below. The YourTableViewController is were you want to implement all the delegate methods.
Note: if you are using a UITableViewController dragged out from the storyboard, it will already have the tableView, and delegate / data source stuff already wired up for you. You will also notice that you are overriding the delegate methods as the UITableViewController class has default implementations of these. If you are just using a normal view controller, then see my previous answer for more details on how to set that up.
import UIKit
class YourTableViewController: UITableViewController {
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
if let cell = cell as? YourCustomTableViewCell {
cell.yourLabel1.text = "some text"
cell.yourLabel2.text = "some other text"
}
return cell
}
}
As others have commented, you really need to provide a little more context.
Here are a few things that might be going wrong, providing more context would confirm or deny this guesses.
First you don't show the numberOfSectionsInTableView method.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
I think you would need to provide a value other than 0
Secondly, since I don't see override in front of what I am sure you are intending to be UITableViewDelegate methods function calls, that means your view controller is not a UITableViewController. This makes me wonder if you defined this view controller as conforming to the UITableViewDelegate protocol and if you set the table view outlet delegate to self. (or even wired up the UITableView to an outlet)
If you use a plain UIViewController to host a table view you need to do the following:
Wire up your UITableView to an outlet in your view controller
Declare the view controller as conforming to the UITableViewDeleagate (and maybe UITableViewDataSource) protocol
set the table view's outlet delegate (and maybe dataSource) properties to self (the view controller implementing the protocols)
Implement the required methods
So something like this:
class MyTableViewController: UIViewController, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("your PrototypeCell", forIndexPath: indexPath)
// Configure the cell...
return cell
}
}
I'm learning Swift and, coding some lines, an error appear on the screen showing: Type JVViewController (class name) does not conform to protocol 'UITableViewDataSource'. I don't know why this appear in my file if I did exactly the same in other apps and I never did this kind of problem.
Please, let me know how to solve this situation.
Thanks!
If a class does not conform with a protocol, it means you need to implement some protocol methods. In case on the UITableViewDataSource, is required to implement the follow methods:
It defines the quantity of tableViewCell you will show on your tableView
tableView(_:numberOfRowsInSection:)
It defines the settings of each tableViewCell
tableView(_:cellForRowAtIndexPath:)
Maybe you forgot to implement one or this two dataSource methods.
E.g.:
class JVViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
...
var collectionItems = ["One", "Two", "Three"]
...
//Quantity of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.collectionItems.count;
}
//Row settings
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.collectionItems[indexPath.row]
return cell
}
...
}
You can find out more in Apple documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/
Numerous tutorials I've been through say the only code I need to display the array I want it to is:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var chatListTableView: UITableView!
var friends = ["Anthony", "Antonio", "Andy"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "ChatListCell")
cell.textLabel.text = self.friends[indexPath.row]
return cell
}
}
However, when I run the app, the tableView is still blank. What am I doing wrong? I feel that I am missing something.
All I want to do is display the array in the tableView.
Check if the ViewController is the datasource and delegate of the tableview
As Aci says in his answer, you have to set the data source and delegate of the table view to your view controller. The easiest way to do that is in Interface Builder.