简体   繁体   中英

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

  • My main VC is connected to its ViewController.class
  • My TableViewCell is connected to its TableViewCell.class
  • I have an identifyer and linked it up, as per code below
  • I linked all the outlets

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
}

}
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
}

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.

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.

class is MyCell : UITableViewCell

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

you should add you tableview in ViewController.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM