gpt4 book ai didi

ios - 显示错误,因为需要符合协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 11:11:29 25 4
gpt4 key购买 nike

这是我的代码:-

型号:-

class QuestionListModel: NSObject {

var optionsModelArray:[OptionsModel] = []

var question:String!

init(dictionary :JSONDictionary) {

guard let question = dictionary["question"] as? String

else {
return

}

if let options = dictionary["options"] as? [String]{
print(options)

print(options)

for values in options{

print(values)

let optionmodel = NH_OptionsModel(values: values)
self.optionsModelArray.append(optionmodel)

}

}
self.question = question
// print(self.dataListArray33)
}
}

选项型号:-

class OptionsModel: NSObject {
var values:String?
init(values:String) {

self.values = values
print( self.values)

}
}

在 View 模型中:-

var questionsModelArray:Array<NH_QuestionListModel>? = []
init(withdatasource newDatasourceModel:NH_QuestionDataSourceModel) {
datasourceModel = newDatasourceModel

print(datasourceModel.dataListArray?.count)

self.questionsModelArray = datasourceModel.dataListArray

print(self.questionsModelArray)

print(datasourceModel.dataListArray)
}


func numberOfSections() -> Int{

return (self.questionsModelArray?.count)!

}

func titleForHeaderInSection(atindexPath indexPath: IndexPath) -> QuestionListModel {

return self.questionsModelArray![indexPath.row]

}

func numberOfRowsInSection(indexPath:IndexPath) -> Int {

if let questionModel = self.questionsModelArray?[indexPath.section]{

return questionModel.optionsModelArray.count

}

else{
return 0

}
}


func datafordisplay(atindex indexPath: IndexPath) -> OptionsModel{

let questionModel = self.questionsModelArray?[indexPath.section]

return questionModel!.optionsModelArray[indexPath.row]

}

在 ViewController 中:-

func numberOfSections(in tableView: UITableView) -> Int {

return questionViewModel.numberOfSections()

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: IndexPath) -> UIView? {


// let headercell = Bundle.main.loadNibNamed("HeaderCell", owner: self, options: nil)?.first as! NH_questionheader


let identifier = "HeaderCell"

var headercell: NH_questionheader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_questionheader

if headercell == nil {
tableView.register(UINib(nibName: "NH_questionheader", bundle: nil), forCellReuseIdentifier: identifier)
headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_questionheader
}

headercell.setReviewData(reviews:questionViewModel.titleForHeaderInSection(atindexPath:section))

return headercell
}



func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 150

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: IndexPath) -> Int {

return questionViewModel.numberOfRowsInSection(indexPath: section)
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
var cell: CellTableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? CellTableViewCell

if cell == nil {
tableView.register(UINib(nibName: "CellTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CellTableViewCell
}
cell.contentView.backgroundColor = UIColor.clear

cell.setOptions(Options1: questionViewModel.datafordisplay(atindex: indexPath))

print("Section \(indexPath.section), Row : \(indexPath.row)")

return cell

}

我的 json 文件:-

{
"data":[
{
"question": "Gender",
"options": ["Male","Female"]

},
{
"question": "How old are you",
"options": ["Under 18","Age 18 to 24","Age 25 to 40","Age 41 to 60","Above 60"]
}, {
"question": "I am filling the Questionnaire for?",
"options": ["Myself","Mychild","Partner","Others"]
}
]
}

这是我的数据。所以我需要在索引单元格中显示标题和选项中的问题。但是显示为错误,因为 UITableview 需要符合 UITableviewDataSource 协议(protocol)。

还显示索引超出范围的错误。怎么办......

最佳答案

我认为您没有为 View Controller 分配数据源。因此,请在 View Controller 的 ViewDidLoad 中分配它

 override func viewDidLoad() {
super.viewDidLoad()
self.yourtableview.delegate = self
self.yourtableview.dataSource = self
// Do any additional setup after loading the view.
}

关于ios - 显示错误,因为需要符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52144416/

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