Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of type "UIViewController" has no member "indexOf"

Tags:

indexing

swift

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
like image 765
sheishistoric Avatar asked Oct 17 '25 04:10

sheishistoric


1 Answers

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)
like image 163
Tamás Sengel Avatar answered Oct 20 '25 14:10

Tamás Sengel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!