I wrote this in Xcode 6 (Swift) but it says "Type 'FirstViewController' does not conform to protocol 'UITableViewDataSource'" and won't let me build the program. Please help?
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.
}
//UIViewTableDataSource
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
}
}
As I wrote in the comments you might as well change the class to a UITableViewController subclass as it is basically the same as UIViewController + UITableViewDelegate + UITableViewDataSource (with a little bit of extra functionality included if you want it). It also has a UITableView property included "out of the box".
You will then end up with the following class:
class FirstViewController: UITableViewController {
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.
}
//UIViewTableDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
override 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 // You can remove ? when updating to XCode 6.1 / Swift 1.1
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
I rewrote your class to work. I deleted a couple of variables I did not need, but you can add them back. Key was to delete 'UITableViewDataSource' (you do not conform to this) and to unwrap the optional cell the way you wrote it. I prefer not to construct the cell that way, but that is another discussion. If you still have issues let me know.
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate {
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.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")!
return cell
}
}
Related
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
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.
}
var alphNames = ["ABC","DEF","GHI","JKL","MNO","PQR","STU"]
func tableView(tableView: UITableView, numberofRowInsection section: Int) -> Int {
return alphNames.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath : indexPath) as UITableViewCell
//Configure the cell...
cell.textLabel?.text = departmentNames[indexPath.row]
return cell
}
}
You made an typo in numberOfRowsInSection function. It is a required function of the UITableViewDataSource protocol. It should be
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return alphNames.count
}
You have typed numberofRowInsection instead.
You have to implement all the required functions. You can see them by command clicking on the UITableViewDataSource. To make sure you don't make any typo, just copy the required functions or start typing in the ViewController the name of the function and autocompletion should do the rest.
This question was asked multiple times.
http://stackoverflow.com/questions/25581780/type-viewcontroller-does-not-confirm-to-protocol-uitableviewdatasource
and I did tried the solutions provided over there but still I am not able to get away with this error. Code to UITableView is below
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
#IBOutlet var tableView: UITableView!
var data: [String] = ["one","two","three","four"]
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.data.count;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
println("You clicked cell number #\(indexPath.row)!")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Please tell what mistake am I doing here.
Try this just remove both the function numberOfRowsInSection and cellForRowAtIndexPath and again add this function like :
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
This is works for me.
The following code didn't 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.
}
}
Please confirm all tableview required methods before the last }. Like
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
var data: [String] = ["one","two","three","four"]
override func viewDidLoad() {
super.viewDidLoad()
self.loadTableView()
// 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 loadTableView() {
var tableObj:UITableView=UITableView()
tableObj=UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
tableObj.delegate=self;
tableObj.dataSource=self;
self.view.addSubview(tableObj)
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section (section)"
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
var nameLabel:UILabel = UILabel(frame: CGRect(x: 50,y: 5,width: 200,height: 30))
nameLabel.text="Title\(indexPath.row)"
nameLabel.textColor=UIColor.brownColor()
nameLabel.font=UIFont.boldSystemFontOfSize(15)
nameLabel.textAlignment=NSTextAlignment.Left
cell.contentView.addSubview(nameLabel)
var imageView:UIImageView=UIImageView(frame: CGRect(x: 5,y: 2,width: 40,height: 40))
imageView.backgroundColor=UIColor.purpleColor()
imageView.layer.masksToBounds=true
imageView.layer.cornerRadius=imageView.frame.width/2
cell.contentView.addSubview(imageView)
return cell
}
}
I have been trying to create a table view using swift using xcode 6 and ios8 but I am not getting anything on my simulator,simply it showing empty(white).
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var myArr = ["one","two","three","four","five"]
#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 myArr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = self.myArr[indexPath.row]
return cell;
}
}
I have even tried by dragging some elements like button, switch, etc, and I am not getting anything on simulator. If I download and run any code its working.
Here my worked code, i dont understand what was wrong in previous one,but i have created a new project then it worked.
i just dragged a table view to viewcontroller and assigned delegate and datasource
then came to viewcontroller.swift and tried
import UIKit
class ViewController: UIViewController {
var items = ["a","b","c"]
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 self.items.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
}
Is dataSource of table set in Interface builder? You can do it in code as well:
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
}
You are missing numberOfSectionsInTableView from the UITableViewDataSource protocol:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
Your 'table' delegate is not set. Add:
table.delegate = self
to your viewDidLoad above
table.dataSource = self
you missed delegate and dataSource please add following lines in viewDidLoad
table.delegate = self
and
table.dataSource = self
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.
So, I'm getting a 'Type ViewController does not conform to protocol UITableViewDataSource' error in Xcode. I've implemented the required functions:
cellForRowAtIndexPath
numberOfRowsInSection
I've even made sure I've told Xcode how many sections there are (which is not required). This is a Single View Application and I have ensured my references are setup correctly in Storyboard. So my Outlets are my view and my table view. My Referencing Outlets are set for dataSource and delegate to be my Table View.
Here's the code.
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var items: [String] = ["We", "Dislike", "Swift", "Very", "Much", "Right", "Now"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1
}
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.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
}
EDIT: Also, I know that I shouldn't need the tableView.delegate and tableView.dataSource as I have assigned them in the Storyboard but I thought I'd include them to make sure you all know that I know to have them set.
UITableViewDataSource protocol has changed a little bit. Try:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Your tableView(tableView:cellForRowAtIndexPath) signature is wrong - it should be:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
Note that it doesn't return an optional value (i.e., no ! suffix) any more.