0

I have the problem that my TableViewController doesn't show the content of the cell(s). I have a button that loads the data which works but should also be shown in the cells. Did I forget something in the "vieDidLoad" function? You can find images from the Storyboard and the app at the end.

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: "TableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
    let btn = UIButton(frame: CGRect(x: 0, y: 25, width: self.view.frame.width, height: 50))
    btn.backgroundColor = UIColor.cyan
    //btn.setTitle("Add new Dummy", for: UIControlState)
    btn.addTarget(self, action: #selector(addDummyData), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btn)
}
@objc func addDummyData() {
    RestApiManager.sharedInstance.getRandomCar { (json: NSArray?) in

        if let results = json {
            for entry in results {
                self.items.append(CarObject(json: entry as! Dictionary))
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
}
func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell
    }
    let car = self.items[indexPath.row]
    print(car.pictureURL)
    if let url = NSURL(string: car.pictureURL) {
        if let data = NSData(contentsOf: url as URL) {
            cell.carImage?.image = UIImage(data: data as Data)
        }
    }

    // Create Swift / Cocoa Touch file with carImage etc. Actionbutton
    cell.carTitleLabel.text = "test"//car1
    cell.carPriceLabel.text = car.carPrice
    print("test3")
    return cell
}

}

Storyboard

app

Skrai2004
  • 39
  • 6
  • Have you added numberOfRowsInSection delegate? – Brijesh Shiroya Aug 29 '17 at 11:19
  • Possible duplicate of [TableView not reloading](https://stackoverflow.com/questions/16681525/tableview-not-reloading) – Yuyutsu Aug 29 '17 at 11:27
  • I tried these: func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.items.count; } – Skrai2004 Aug 29 '17 at 11:33
  • I am using a UITableViewController so it says that declaring datasource and delegate is redundant. I tried it. – Skrai2004 Aug 29 '17 at 11:35

3 Answers3

2

Like Rishabh said, you need to set datasource and delegate in viewDidLoad

override func viewDidLoad {
   super.viewDidLoad()
   tableView.datasource = self
   tableView.delegate = self
}
swift2geek
  • 1,697
  • 1
  • 20
  • 27
  • Sorry I forgot to put in this line "class TableViewController: UITableViewController" in the code snippet. So this becomes redundant. – Skrai2004 Aug 29 '17 at 11:25
0

You need to set DataSource and Delegate in your viewDiDLoad() Something like below.

 self.tableView.dataSource = dataSource!
Rishabh Dugar
  • 606
  • 5
  • 16
0

You should have these functions for a default UITableViewController. Maybe you missed one of them.

import UIKit

class YourViewController: UITableViewController {

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1 //Your section count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count //Your row count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell
    }
    let car = self.items[indexPath.row]
    print(car.pictureURL)
    if let url = NSURL(string: car.pictureURL) {
        if let data = NSData(contentsOf: url as URL) {
            cell.carImage?.image = UIImage(data: data as Data)
        }
    }

    // Create Swift / Cocoa Touch file with carImage etc. Actionbutton
    cell.carTitleLabel.text = "test"//car1
    cell.carPriceLabel.text = car.carPrice
    print("test3")
    return cell
}

}

Note: You should also check your self.items.count in numberOfRowsInSection. Your return value should not be 0.

kkakkurt
  • 2,790
  • 1
  • 30
  • 33
  • Thanks I just tried this. It might have been solved but I'm receiving another error message: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'TableViewCell'' – Skrai2004 Aug 29 '17 at 11:47
  • Did you set your TableViewCell's identifier in storyboard? – kkakkurt Aug 29 '17 at 11:52
  • You should open your Storyboard, select your TableViewCell, open Attributes Inspector in right menu, find Table View Cell section and add "TableViewCell" in Identifier text field. – kkakkurt Aug 29 '17 at 11:54
  • It can be occur if you renamed your xib or storyboard file outside the xcode. You can see possible fixing ways in these links: https://stackoverflow.com/questions/27842740/nsinternalinconsistencyexception-reason-could-not-load-nib-in-bundle and https://stackoverflow.com/questions/12722461/nsinternalinconsistencyexception-reason-could-not-load-nib-in-bundle-nsbun – kkakkurt Aug 29 '17 at 12:29