type 'ThirdViewController' does not conform to protocol UITableViewDataSource - swift

I have started my first app, and I want the app to have a view that works as a to-do list, however, I'm getting type 'ThirdViewController' does not conform to protocol UITableViewDataSource as an error in my code. I already looked at a similar thread but found no solution there
import UIKit
class ThirdViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var list = ["Math Homework", "English Project", "Bio Quiz"]
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return list.count
}
public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
}

As specified in the Apple Developer documentation, you need the following methods:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
You instead used these:
public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Remove public from both methods and it should work. Also, add _ before tableView:.
Reference:
https://developer.apple.com/reference/uikit/uitableviewdatasource
EDIT:
The methods that I described are for Swift 3. In case you are using Swift 2.3, these should be your methods:
func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Almost the same that you used in the first place, but without public.

Implement the required protocol methods for UITableViewDataSource and UITableViewDelegate.
you are missing;
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

Related

How to use SkeltonView using RxSwift, RxCocoa

I wanna use SkeltonView in my app. But, it was described that we need to confirm to SkeltonTableViewDataSource to use it in README.
Is there any one who knows to use SkeltonView like tableView.rx.items.
If you want to show the skeleton in a UITableView, you need to conform to SkeletonTableViewDataSource protocol.
public protocol SkeletonTableViewDataSource: UITableViewDataSource {
func numSections(in collectionSkeletonView: UITableView) -> Int // Default: 1
func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier
func collectionSkeletonView(_ skeletonView: UITableView, skeletonCellForRowAt indexPath: IndexPath) -> UITableViewCell? // Default: nil
func collectionSkeletonView(_ skeletonView: UITableView, prepareCellForSkeleton cell: UITableViewCell, at indexPath: IndexPath)
}

UITableView does not conform to protocol

Type ‘TVViewController’ does not conform to protocol ‘UITableViewDataSource’
I'm fairly new to programming and the error is very vague so I don't know where to begin troubleshooting.
class WeeksViewController: UIViewController, UITableViewDataSource {
//Categories
var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
//Number of sections
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}
//Defining the headers
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
//Number of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//Cell as identifier & category as the class
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow
return cell
}
}
You copied and pasted Swift 2 code. These are the proper Swift 3+ methods
class WeeksViewController: UIViewController, UITableViewDataSource {
//Categories
var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
//Number of sections
func numberOfSections(in tableView: UITableView) -> Int
return categories.count
}
//Defining the headers
func func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
//Number of rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//Cell as identifier & category as the class
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", for: indexPath) as! CategoryRow
return cell
}
}
It's always worth it to have a look at the documentation
I think the issue is that you have a misspelling in the name of the numberOfRows data source method and the cellForRow signature is also a bit off.
Instead of these:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}
...
func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//
}
You probably want these:
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// code
}

UITableView inside UIViewController

I'm trying to use UITableView Inside UIViewController. However, when I tried to so it it gives me an error when I start the app.
the error says "method doesnt override ant method from superclass"
import UIKit
class GPATableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectsArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
objectsArray =
[Objects(sectionName: "Section1" , sectionObjects: ["","",""]),
Objects(sectionName: "Section2" , sectionObjects: ["","",""]),
Objects(sectionName: "Section3" , sectionObjects: ["","",""])]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
// cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
print
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
}
When you implement the UITableViewDatasource in an UIViewController, you are not overriding the methods.
Remove override from the methods that the compiler is tell you to.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
// cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
print
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
NOTE: If you were using a UITableViewController, then you would need the override, which is probably what was going on in whatever file you copied this from.
Functions involving tableView such as numberOfSections(...), cellForRowAt etc. are not part of the UIViewController.
Instead, they belong to the UITableViewDelegate and UITableViewDataSource.
To fix your errors, remove the override keyword in front of these functions. You are not overriding them but instead "implementing" them at the request of the protocols.
Be sure to also set the view controller as the delegate and dataSource if you have not already done so in a Storyboard:
override func viewDidLoad()
super.viewDidLoad()
// other code...
tableView.delegate = self
tableView.dataSource = self
// other code...
}

Swift 3 UITableView sections showing after the list

I'm trying to create a list of products using swift 3 UITableViewController, but the sections is showing after the list of the products. There must be something I'm missing, couldn't find anywhere
Here is a simplified code
let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"], ["2,0", "2,1", "2,2"], ["3,0", "3,1", "3,2"]]
override func numberOfSections(in tableView: UITableView) -> Int {
return data.count//productCategoriesSections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count//productCategories[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCategoryCell", for: indexPath) as! CategoryDefaultTableViewCell
cell.titleTextView.text = data[indexPath.section][indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "Section \(section)"
}
screenshot
It is because you are using wrong callback.
Use override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? instead of override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?.
The former will place the text as a header above the section. The latter places it at the bottom.

Can't override UITableViewDataSource and UITableViewDelegate

extension FormViewController : UITableViewDelegate {
public func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
}
extension FormViewController : UITableViewDataSource {
public func numberOfSectionsInTableView(tableView: UITableView) -> Int
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
}
class NotifiableFormViewController: FormViewController
Why can't I implement and override the DataSource and TableViewDelegate? Error:"Method does not override any of it's superclass method"
class FriendsTableViewController: NotifiableFormViewController{
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
}
more simply, remove the word override because you're not overriding the method, you are implementing it
UITableViewDataSource and UITableViewDelegateare just protocol
If you want override it, you implement above functions in NotifiableFormViewController
then override in your FriendsTableViewController
but if not implement in your super class, then you can't override it (nothing override implemented delegate or datasource function)
By definition, extensions can only add functionality to existing classes. It can not change, that includes overriding, the existing implementation.