edited for new issue: Instance member 'questionsList' of type 'QuestionController' cannot be used on instance of nested type 'QuestionController.QuestionController'
Can someone explain a quick fix? I'm still learning Swift so not quite sure of what it's asking me to do.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let index = navigationController?.viewControllers.indexOf()->() {
let question = questionsList[index]
if let count = question.answers?.count {
return count
}
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath) as! AnswerCell
if let index = navigationController?.viewControllers.indexOf() {
let question = questionsList[index]
cell.nameLabel.text = question.answers?[indexPath.row]
}
new issue:
error first appears at line 47:
import UIKit
struct Question {
var questionString: String?
var answers: [String]?
var selectedAnswerIndex: Int?
}
class QuestionController: UITableViewController {
let cellId = "cellId"
let headerId = "headerId"
var questionsList: [Question] = [Question(questionString: "What is your favorite type of food?", answers: ["Sandwiches", "Pizza", "Seafood", "Unagi"], selectedAnswerIndex: nil), Question(questionString: "What do you do for a living?", answers: ["Paleontologist", "Actor", "Chef", "Waitress"], selectedAnswerIndex: nil), Question(questionString: "Were you on a break?", answers: ["Yes", "No"], selectedAnswerIndex: nil)]
class QuestionController: UITableViewController {
let cellId = "cellId"
let headerId = "headerId"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Question"
navigationController?.navigationBar.tintColor = UIColor.white
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
tableView.register(AnswerCell.self, forCellReuseIdentifier: cellId)
tableView.register(QuestionHeader.self, forHeaderFooterViewReuseIdentifier: headerId)
tableView.sectionHeaderHeight = 50
tableView.tableFooterView = UIView()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let index = navigationController?.viewControllers.index(of: self) {
let question = questionsList[index]
if let count = question.answers?.count {
return count
If you want to get the index of the current view controller, you should give it as an input. Also, indexOf
is firstIndex(of:)
since Swift 5.
let index = navigationController?.viewControllers.firstIndex(of: self)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With