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!
Related
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.
First of all, I'm not sure the name of it is "extension view", please look at the screenshot, I mean the view with the yellow background.
I have made a custom table view in this view, and I did all the configurations, the problem is, in the end, I have to add delegate and dataSource for it.
Let's call the table cell inside of the view "OptionTableView".
I wrote these lines the viewdidload of the view controller (the controller that is inside the iPhone frame in the screenshot),
override func viewDidLoad() {
super.viewDidLoad()
OptionTableView.delegate = self
OptionTableView.dataSource = self
}
but it crashes the app with this error:
Thread 1: signal SIGABRT
Because it's UiView, I can't make a new file with a type of UIControlView for it.
Could you help me? How I can add delegate and dataSource for this extension UIView or whatever is called, that let me to use tableview inside it?
Thank you so much for your help
I think you not add protocol of tableview. I write a code for this:-
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var table_view: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table_view.dataSource = self
table_view.delegate = self
}
}
I am trying to override the UITableViewDelegate methods inside extension.The base class has already implemented the methods. Please find the details below:
Base Class:
class BaseTableViewController:UITableViewDelegate,UITableViewDataSource{
//Base Class. The delegate and datasource methods has been implemented
}
class ChildViewController: BaseTableViewController{
//Inherited class
}
extension ChildViewController {
//Trying to override the Tableview Delegate and Datasource methods but getting error.
}
Error Detail:
I am trying to do the conversion from Swift 3.0 to Swift 4.0.
The implementation was working fine with Swift 3.0 but got error in Swift 4.0.
I have looked into below links:
Override non-dynamic class delaration
Please suggest the right approach for the above implementation.
The right approach seems to override methods from protocols inside the class and not into an extension:
class BaseTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
}
}
class ChildViewController: BaseTableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
}
}
Extensions are meant to add new functionalities, not override existing ones. Another approach would be to delegate those functions on an external object.
HogeViewController.swift
protocol HogeDelegate: class {
func huga()
}
class HogeViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
weak var delegate:
func tableView(didSelectRowAt){
self.delegate?.huga()
}
}
HogeTableViewCell.swift
class HogeTableViewCell: UITableViewCell, HogeDelegate{
func huga(){
print("huga")
}
}
don't run this code...
Is this writing impossible?
func tableView(didSelectRowAt) isn't the signature of the UITableViewDelegate's method, so no, it's never getting called. Your function's signature needs to be
func tableView(_ tableView: UITableView, didSelectRowAt rowIndex: Int)
Even better, instead of having your class extend UIViewController and implement the UITableView data source and delegate, just make your class extend UITableViewController, which already implements the data source and delegate, and will thus force you to use the override modifier, like
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
Since the cell is variable it seems to be a specification that can not implement methods via delegate.
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.