如何为这种类型的api创建模型,viewModel和DataSourceModel。
{"data":[{"id":45,"question":"Were the answers that staff provided to your questions presented in a way that you could understand?","options":["Yes, always","Yes, sometimes","No""Other"]}]}
这是我的api。
根据我的设计,我正在使用tableview部分,因此将在标题中显示问题,并在tableviewcell中显示选项列表。
输出为:-
Were the answers that staff provided to your questions presented in a way that you could understand?
Yes, always
Yes, sometimes
No
Other
这样我需要显示。
我的代码如下:-
型号:-
classQuestionListModel: NSObject {
var home:[OPTIONS] = []
var id:String?
var question:String?
var options:[String]?
var v:String?
init?(dictionary :JSONDictionary) {
guard
let question = dictionary["question"] as? String,
let id = dictionary["id"] as? String
else {
return
}
if let options = dictionary["options"] as? [String]{
print(options)
}
self.question = question
self.id = id
}
}
viewmodel:-
func numberOfSections(tableView: UITableView) -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}
func titleForHeaderInSection(atsection section: Int) -> QuestionListModel {
return datasourceModel.dataListArray![section]
}
func numberOfRowsInSection(section:Int) -> Int {
print(self.tableArray[section].count)
return self.tableArray[section].count
}
func datafordisplay(atindex indexPath: IndexPath) -> OPTIONS{
// print(datasourceModel.dataListArray![indexPath.section].options)
return datasourceModel.dataListArray![indexPath.row]
// return values
}
在ViewController中:-
func numberOfSections(in tableView: UITableView) -> Int {
return reviewViewModel.numberOfSections(tableView: tableView)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let identifier = "HeaderCell"
var headercell: questionheader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? questionheader
if headercell == nil {
tableView.register(UINib(nibName: "questionheader", bundle: nil), forCellReuseIdentifier: identifier)
headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? questionheader
}
headercell.setReviewData(reviews:reviewViewModel.titleForHeaderInSection(atsection:section))
headercell.setReviewData(reviews:reviewViewModel.datafordisplay(atindex: section))
return headercell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 63
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reviewViewModel.numberOfRowsInSection(section: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
var cell: QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
if cell == nil {
tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
}
cell.contentView.backgroundColor = UIColor.clear
cell.question.text = reviewViewModel.datafordisplay(atindex: indexPath)
print(reviewViewModel.tableArray)
return cell
}
在问题列表单元格中:-
class
QuestionListCell: UITableViewCell {
@IBOutlet weak var imagebutton: UIButton!
@IBOutlet weak var options: UILabel!
var i = 0
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setReviewData(reviews:QuestionListModel)
{
print(reviews.options)
self.question.text = reviews.v
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
答案 0 :(得分:0)
code128(u'X001SB7OYL', writer=ImageWriter())
需要一个字符串,并且您传递了cell.question.text
。
应该是
QuestionListModel