clydececiljohn
clydececiljohn

Reputation: 27

How to get the value of the object

This is my model.

 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

                       }

In viewcontroller the cellforrowatindex is as below.

 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()
        }

}

So according to this

`Print(model.answerType) = ProjectName.AnswerType.AnswerRadioButton`

I have Tableview.In that different types of the cell are used to display . From this i need to get only the word AnswerRadioButton So that I can go to switch case.If the TYPE is AnswerRadioButton display the cell1.ELSE each for type the cell should view in the tableview

How to do?

Upvotes: 0

Views: 100

Answers (2)

Ashley Mills
Ashley Mills

Reputation: 53121

You should be switching on model.answerType, not it's rawValue. In the answer below, note the suffix ? on each case is required as model.answerType is optional.

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()
}


If you declare

var answerType: NHAnswerType = .AnswerRadioButton //or some other default case

then you can remove ? suffix, and the default: case…

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
}


Also, your enum should really remove the repeated Answer and begin your cases with lowercase…

enum AnswerType:Int
{
    case radioButton = 1
    case checkboxButton
    case smileyButton
    case starRatingButton
    case textButton
}

Upvotes: 0

Paulw11
Paulw11

Reputation: 114866

You want to dequeue the appropriate cell based on the AnswerType

Probably the simplest way is to use a String rawValue and set your cell re-use identifiers to these strings (note that by convention, enumeration cases should start with a lower case letter and the word answer is redundant)

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
}

There are a couple of issues with this though:

  1. You are using integer raw values in your JSON and you may not have the ability to change that.
  2. You probably need to customise each cell differently based on the content, so you can't use a single dequeue.

So, you could use a switch, something like you have, but you can make it simpler and clearer by using the enumeration cases rather than raw values:

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()
    }
}

Upvotes: 1

Related Questions