这是我的模型。
enum AnswerType:Int
{
case AnswerRadioButton = 1
case AnswerCheckboxButton
case AnswerSmileyButton
case AnswerStarRatingButton
case AnswerTextButton
}
class NH_QuestionListModel: NSObject {
var dataListArray33:[NH_OptionsModel] = []
var id:Int!
var question:String!
var buttontype:String!
var options:[String]?
var v:String?
var answerType:NHAnswerType?
var optionsModelArray:[OptionsModel] = []
init(dictionary :JSONDictionary) {
guard let question = dictionary["question"] as? String,
let typebutton = dictionary["button_type"] as? String,
let id = dictionary["id"] as? Int
else {
return
}
// (myString as NSString).integerValue
self.answerType = AnswerType(rawValue: Int(typebutton)!)
print(self.answerType?.rawValue)
if let options = dictionary["options"] as? [String]{
print(options)
print(options)
for values in options{
print(values)
let optionmodel = OptionsModel(values: values)
self.optionsModelArray.append(optionmodel)
}
}
self.buttontype = typebutton
self.question = question
self.id = id
}
在viewcontroller中,cellforrowatindex如下所示。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.row)
// print(model.answerType?.hashValue)
print(model.answerType)
print(model.answerType?.rawValue)
// questionViewModel.items = model.answerType
let value = model.answerType?.rawValue
let v = model.answerType
// let item = model.answerType
switch (value) {
case 0.0:
if let cell = tableView.dequeueReusableCell(withIdentifier: NH_RadioTypeCell.identifier, for: indexPath) as? NH_RadioTypeCell {
// cell.item = item
return cell
}
return UITableViewCell()
}
}
所以根据这个
`Print(model.answerType) = ProjectName.AnswerType.AnswerRadioButton`
我有Tableview.In,不同类型的单元格用于显示。
从这里我只需要得到单词AnswerRadioButton以便切换大小写。如果TYPE是AnswerRadioButton,则显示cell1.ELSE每个类型的单元格都应该在表格视图中查看
怎么做?
最佳答案
您要基于AnswerType
使适当的单元出队
可能最简单的方法是使用String
rawValue
并将单元格重用标识符设置为这些字符串(请注意,按照惯例,枚举大小写应以小写字母开头,而单词answer
是多余的)
enum AnswerType:String
{
case radioButton = "RadioButton"
case checkboxButton = "CheckboxButton"
case smileyButton = "SmileyButton"
case starRatingButton = "StarRatingButton"
case textButton = "TextButton"
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.row)
let answerType = model.answerType ?? .textButton // Why is answerType an optional? Surely questions *must* have an answerType
let cell = tableView.dequeueReusableCell(withIdentifier:answerType.rawValue, for: indexPath)
return cell
}
但是有两个问题:
您在JSON中使用整数原始值,因此可能无法更改它。
您可能需要根据内容不同地自定义每个单元,因此不能使用单个出队。
因此,您可以使用类似的开关,但是可以通过使用枚举用例而不是原始值来使其更简单明了:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.row)
switch (model.answerType) {
case .radioButton:
let cell = tableView.dequeueReusableCell(withIdentifier: NH_RadioTypeCell.identifier, for: indexPath) as! NH_RadioTypeCell
return cell
case .checkboxButton:
let cell = tableView.dequeueReusableCell(withIdentifier: NH_CheckboxTypeCell.identifier, for: indexPath) as! NH_CheckboxTypeCell
return cell
...
default:
return UITableViewCell()
}
}
关于ios - 如何获得对象的值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52512429/