这是我的模特。
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每个类型的单元格都应该在表格视图中查看
怎么办?
答案 0 :(得分:0)
您应该打开model.answerType
,而不是rawValue
。在下面的答案中,请注意每种情况下的后缀?
是必需的,因为model.answerType
是可选的。
switch model.answerType {
case AnswerRadioButton?:
// OK to force unwrap here as it's a programmer error if it fails
return tableView.dequeueReusableCell(withIdentifier: NH_RadioTypeCell.identifier, for: indexPath) as! NH_RadioTypeCell
case AnswerCheckboxButton?:
// return cell
case // etc
default:
return UITableViewCell()
}
如果您声明
var answerType: NHAnswerType = .AnswerRadioButton //or some other default case
然后您可以删除?
后缀和default:
大小写…
switch model.answerType {
case AnswerRadioButton:
// OK to force unwrap here as it's a programmer error if it fails
return tableView.dequeueReusableCell(withIdentifier: NH_RadioTypeCell.identifier, for: indexPath) as! NH_RadioTypeCell
case AnswerCheckboxButton:
// return cell
case // etc
}
另外,您的枚举应真正删除重复的Answer
,并以小写开头……
enum AnswerType:Int
{
case radioButton = 1
case checkboxButton
case smileyButton
case starRatingButton
case textButton
}
答案 1 :(得分:0)
您要基于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
}
这有两个问题:
因此,您可以使用类似的开关,但是可以通过使用枚举用例而不是原始值来使其更简单明了:
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()
}
}