根据选择和默认状态设置按钮图像

时间:2018-09-27 06:08:04

标签: ios swift uitableview uibutton

这是我的代码。

class NamePictureCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel?
    @IBOutlet weak var buttonType: UIButton?

    func setOptions(Options1:NH_OptionsModel)

    {
        self.nameLabel?.text = Options1.values

    }

//    var item: NH_QuestionListModel? {
//       didSet {
//             self.nameLabel?.text = item?.buttontype
//          }
//    }

    static var nib:UINib {
        return UINib(nibName: identifier, bundle: nil)
    }

    static var identifier: String {

        return String(describing: self)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

         }

    override func prepareForReuse() {
        super.prepareForReuse()

         }    
}

在viewController中:-

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



        let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)

        // print(model.answerType?.hashValue)

        print(model.answerType)

        print(model.answerType?.rawValue)



        switch model.answerType {

        case .NHAnswerRadioButton?:



            if let cell = self.tableview.dequeueReusableCell(withIdentifier: NamePictureCell.identifier) as? NamePictureCell {


             //   cell.item = model
             cell.setOptions(Options1:questionViewModel.datafordisplay(atindex: indexPath))

                //   print(cell.item)

                return cell

            }

        case .NHAnswerCheckboxButton?:

            if let cell = self.tableview.dequeueReusableCell(withIdentifier: AboutCell.identifier, for: indexPath) as? AboutCell {

                cell.setOptions(Options1:questionViewModel.datafordisplay(atindex: indexPath))                            //  cell.item = item

                return cell

            }

        case .NHAnswerSmileyButton?:

            if let cell = self.tableview.dequeueReusableCell(withIdentifier: FriendCell.identifier) as? FriendCell{

                cell.textLabel?.text = ""

                return cell



            }

        case .NHAnswerStarRatingButton?:

            if let cell = self.tableview.dequeueReusableCell(withIdentifier: EmailCell.identifier) as? EmailCell {

                cell.textLabel?.text = ""

                return cell

            }



        case .NHAnswerTextButton?:

            if let cell = self.tableview.dequeueReusableCell(withIdentifier:AttributeCell.identifier, for: indexPath) as? AttributeCell{

                cell.textLabel?.text = ""

                //  cell.item = item

                return cell

            }

        default:

            return UITableViewCell()

        }

        return UITableViewCell()

    }

模型是:-

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:[NH_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 = NHAnswerType(rawValue: Int(typebutton)!)

        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.buttontype = typebutton
        self.question = question
        self.id = id

                   }

}

在NamePictureCell的表格视图单元中,我有一个标签和一个单选按钮。

在此单元的xib中。我为按钮设置了以下详细信息。

在默认情况下-给出一张图片, 选中时-提供另一张图片

所以我的问题是根据我的api-

按钮类型-1,然后在正常状态下应显示默认图像,并且在选择按钮时应显示所选图像。
怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码显示默认按钮和选定按钮的图像:

buttonType.setImage(UIImage(named: "defaultImage")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
    buttonType.setImage(UIImage(named: "selectedImage")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)