Reputation: 39
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
}
}
Upvotes: 0
Views: 449
Reputation: 2800
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.
Upvotes: 0
Reputation: 1812
Like Rishabh said, you need to set datasource and delegate in viewDidLoad
override func viewDidLoad {
super.viewDidLoad()
tableView.datasource = self
tableView.delegate = self
}
Upvotes: 2
Reputation: 606
You need to set DataSource and Delegate in your viewDiDLoad() Something like below.
self.tableView.dataSource = dataSource!
Upvotes: 0