首页 > 解决方案 > 根据选择和默认状态设置按钮图像

问题描述

这是我的代码。

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

         }    
}

在视图控制器中:-

 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 的 tableview 单元格中,我有一个标签和一个单选按钮。

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

在默认情况下 - 给出一个图像,在选定时 - 给出另一个图像

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

button type - 1 然后在正常状态下应该显示默认图像,并在选择按钮时显示所选图像。
怎么做?

标签: iosswiftuitableviewuibutton

解决方案


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

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

推荐阅读