gpt4 book ai didi

swift - 类型 "UIViewController"的值没有成员 "indexOf"

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:01 28 4
gpt4 key购买 nike

针对新问题进行了编辑:“QuestionController”类型的实例成员“questionsList”不能用于嵌套类型“QuestionController.QuestionController”的实例

有人可以解释一下快速修复吗?我仍在学习 Swift,所以不太确定它要求我做什么。

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]
}

新问题:

错误首先出现在第 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

最佳答案

如果你想得到当前 View Controller 的索引,你应该把它作为输入。此外,自 Swift 5 以来,indexOffirstIndex(of:)

let index = navigationController?.viewControllers.firstIndex(of: self)

关于swift - 类型 "UIViewController"的值没有成员 "indexOf",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49878469/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com