Why doesn't tableview delegate work? - uitableview

I have the following class in a singleview app. In IB, I have connected the datasource and datadelegate to the tableview. Is there something I'm doing wrong that none of the tableview delegates fire?
The cell identifier is set in IB.
import UIKit
class ViewController: UITableViewController {
let myarray = ["item 1", "item 2", "item 3", "item 4"]
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.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel?.text = myarray[indexPath.item]
return cell;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
}
There is already a prototype cell in IB.
One other thing, viewDidLoad() doesn't even fire.

You are supposed to dequeue cells in cellForRowAtIndexPath. In storyboard, add a UITableViewCell to the table if there isn't one already, and you should see a prototype cell. Make sure that the identifier of the prototype is "test" like it is in your code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("test") as UITableViewCell
cell.textLabel?.text = self.myarray[indexPath.row]
return cell
}

Related

not calling didSelectRowAtIndexPath method in Tableview

I have embedded UITabbar in UIViewController and then I put some UITabbarItems in my UITabbar. I also created another UIViewController with UITableView embedded in it.
I have linked one of the UITabbarItem with this newly created UIViewController(with UITableView in it).
When I run the program and tap on that UITabbarItem it shows a table view but when I select any row of table, the data displaying in UITableView disappears.
I then debug the code and found that didSelectRowAtIndexPath method didn't call.
I searched on the internet and find this but cannot understand it well.
I tried by implementing both types of tableViews (UITableViewController,UIViewController with UITableView in it) but still same problem.
I am stuck here. Anyone please help me. How to get resolve this issue.
I will be very Thankful to you.
Here is my swift code
import UIKit
class DashboardViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var selectedIndexPathForCheckMark:NSIndexPath?
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("MyCell")!UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
selectedIndexPathForCheckMark = indexPath
tableView.reloadData()
}
here are its snapshots
Second UITabbarItem from right implements UITableView
Follow Steps:
1. Take A view controller and add tableview.
2. set outlet with MytabelView
3. set Datasource and delegate of tableview to ViewController
4. and put the following code in ViewController class
5. Take a custom tableview cell with name MyCell having cell identifier "MyCellid"
6. Create swift file with name MyCell, make it subclass of UITableViewCell
7. Choose custom cell and assign it class to MyCell.
I think Its done
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var MytableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.MytableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCellid")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.MytableView.dequeueReusableCellWithIdentifier("MyCellid")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
}

Why I've set up a prototype cell but nothing showed up?

class DemoTableController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var answerView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
answerView.delegate = self
answerView.dataSource = self
answerView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
return cell
}
I add the tableView to a view in a ViewController via StoryBoard.
And then I add a button to my prototype cell. The blank tableView does show up , but it's without the button.
Thank you in advance.
Delete this line,your code should work.Here you do not need registerClass
answerView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
Please, make sure you correctly set your identifier within your storyboard.
It has to be the same that in your line below :
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
Also, you probably won't need to use the registerClass method on your tableView (in your viewDidLoad).

Having trouble responding to selections when using a UITableView and swift

I have a question about responding to selections with a UITableView. I want to create a FAQ and trigger an action when the user selects a row. I'm just testing it right now, and set it to print a log message when the user selects one of the rows on the table view. However, in the simulator nothing happens.
I'm not sure what I am doing wrong. Can someone please help enlighten me?
Thanks in advance. Below is my code.
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let iEdCafeVid = [
("temp", "temp"),
("temp", "temp"),
("temp", "temp") ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return iEdCafeVid.count //the number of spots in the array
}
//uses the numberOfRowsInSection method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell //because creating as AnyObject.
//var cell = UITableViewCell() //instantiating class, but should use the dequeue option.
//need to add the prototype cell in the storyboard before the dequeue option will work.
//creating touple (desciptions of the cell content parts)
let (videoTitle, subtitle) = iEdCafeVid[indexPath.row] //index parth is the value (i.e. the array in this case) that we created in the numberOfRowsInSection method.
cell.textLabel.text = videoTitle
cell.detailTextLabel?.text = subtitle
//retrieve image
var cellImage = UIImage(named: "CellIcon")
cell.imageView.image = cellImage
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("user has selected a cell")
}
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.
}

UITableView and UITableViewDataSource

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.

cellForRowAtIndexPath is never called

I have a storyboard which contains UIViewController which has UITableview and UITableViewCell, everything is hooked up properly and I have put all the required code for UIViewController.
When I run the app in the simulator one of the datasource function cellForRowAtIndexPath is never called but numberOfRowsInSection is called
"func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int" is called
But "func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell" IS NEVER CALLED
Below is my code for view controller:
import UIKit
class DVViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
#IBOutlet var tableView : UITableView?
var tableData = ["Row 1", "Row 2", "Row 3"]
let kCellID = "cell"
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView?.delegate = self;
self.tableView?.dataSource = self;
//load cell
self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellID);
self.tableView?.reloadData();
}
//MARK: Tableview delegates
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.tableData.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
//var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellID)
let cell : UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: kCellID);
return cell;
}
}
Can someone suggest why cellForRowAtIndexPath is never called?
Here is what I did, uninstalled Xcode 5 & 6 both the reinstalled Xcode 6 only, Ran the project and it WORKED. After 2 days of debugging its the reinstall which did the trick.
I have just checked your code, it works.
Check to see whether or not numberOfRowsInSection is called. if not check the connection in storyboard.
Try this:
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
func viewDidLoad(){
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}

Resources