Brewski
Brewski

Reputation: 684

UITableView not loading data

My data is not loading in my UITableView and I'm not sure why, as far as I can tell I've done everything by the book.

I created a TableView and a TableViewCell in storyboard, added an image and three labels.

Structure is: ViewController > TableView > TableViewCell > Image, Labels

It's not even loading my NIB, on account that I'm not getting any output in the output screen?

import UIKit

class MyCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDate: UILabel!
@IBOutlet weak var cellDescription: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cellImage: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()

    print ("Did Load")

    tableView.delegate = self
    tableView.dataSource = self

}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 7
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCellID", for: indexPath)
    return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 70
}

}

Upvotes: 0

Views: 1742

Answers (5)

J. Doe
J. Doe

Reputation: 13033

import UIKit

class MyCell: UITableViewCell{ //<-- make sure your cell has this class and connect the outlets

@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDate: UILabel!
@IBOutlet weak var cellDescription: UILabel!
@IBOutlet weak var tableView: UITableView! //<-- what is this doing here?? remove this
@IBOutlet weak var cellImage: UIImageView!

}

class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

class Cell{
let title: String
//do this for every UIView in your cell
init(title: String //continue...){
self.title = title
//continue
}
}
var cells = [Cell]()

@IBOutlet weak var myTableView //<-- set here the outlet
override func viewDidLoad() {

    print ("Did Load")
    cells.append(Cell(//initialize))
    myTableView.delegate = self
    myTableView.dataSource = self
    myTableView.reloadData()

}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 7
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCellID", for: indexPath) as! MyCell //<-- ADDED
    cell.cellTitle = cells[indexPath.row].title
    //continue
    return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 70
}

Upvotes: 1

Kwaku Eshun
Kwaku Eshun

Reputation: 149

Setting the delegate and data source should be in a view controller. The tableView outlet and all delegate and data source methods should also be in a view controller. try to create a configure cell method in your cell class where you set the data you are going to use to update the UI objects in your in your cell content view and create the cell - type cast as your custom class - in cell for row at method in view controller class.

Upvotes: 1

Abhilasha
Abhilasha

Reputation: 76

You need to register the table cell class or nib to the tableview. Verify that if you have done this or not.

Upvotes: 0

Bhavin Ranpara
Bhavin Ranpara

Reputation: 280

all delegate and datasource method of tableview **[**func numberOfSections(in tableView: UITableView) -> Int,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell,

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat**]**

must in viewcontroller and not in tableviewcell.

Upvotes: 1

RGB_DS
RGB_DS

Reputation: 118

class is MyCell : UITableViewCell

but you say Structure is: ViewController > TableView > TableViewCell > Image, Labels.

you should add you tableview in ViewController.

Upvotes: 0

Related Questions