Hi, This is one of my first projects and I am stuck, my variable "quotes" is saved as a bunch of quotes in a list, I am trying to make the amount of columns in a table be the number of quotes and the tables boxes have the quote inside them as their text. :) thanks
my code:
{code}
import UIKit
class QuoteTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return quotes.count
}
override func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell ()
cell.textLabel?.text = quotes[indexPath.row]
return cell
}
class QuoteTabelViewController: UITableViewController {
var quotes = [
":)",
":(",
]
}
{code}
my code:
{code}
import UIKit
class QuoteTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return quotes.count
}
override func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell ()
cell.textLabel?.text = quotes[indexPath.row]
return cell
}
class QuoteTabelViewController: UITableViewController {
var quotes = [
":)",
":(",
]
}
{code}
Please, when you paste code, use Paste And Match Style to avoid all the blank lines that make code very hard to read.
And use the code formatter tool (<>)
Now, for the errors:
quotes is defined in another class (QuoteTabelViewController), so it is unknown in QuoteTableViewController
Is it on purpose that one class is called QuoteTableViewController and the other QuoteTabelViewController : if so, that's very confusing.
But I understand that it should be the same class.
In addition, signatures of delegate func are wrong, they miss a starting underscore:
In cellForRow, you should not create a cell, but dequeue.
Assuming cell ID is CellItem
let cell = tableView.dequeueReusableCell(withIdentifier: "CellItem", for: indexPath)
Hence code should be (delete QuoteTabelViewController):
And use the code formatter tool (<>)
Code Block import UIKit class QuoteTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return quotes.count } override func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell () cell.textLabel?.text = quotes[indexPath.row] return cell } class QuoteTabelViewController: UITableViewController { var quotes = [ ":)", ":(", ] }
Now, for the errors:
quotes is defined in another class (QuoteTabelViewController), so it is unknown in QuoteTableViewController
Is it on purpose that one class is called QuoteTableViewController and the other QuoteTabelViewController : if so, that's very confusing.
But I understand that it should be the same class.
In addition, signatures of delegate func are wrong, they miss a starting underscore:
Code Block override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
In cellForRow, you should not create a cell, but dequeue.
Assuming cell ID is CellItem
let cell = tableView.dequeueReusableCell(withIdentifier: "CellItem", for: indexPath)
Hence code should be (delete QuoteTabelViewController):
Code Block import UIKit class QuoteTableViewController: UITableViewController { var quotes = [ ":)", ":(", ] override func viewDidLoad() { super.viewDidLoad() } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return quotes.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // REMOVE THIS. let cell = UITableViewCell () let cell = tableView.dequeueReusableCell(withIdentifier: "CellItem", for: indexPath) cell.textLabel?.text = quotes[indexPath.row] return cell } }