ios - 根据选择和默认状态设置按钮图像
问题描述
这是我的代码。
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 然后在正常状态下应该显示默认图像,并在选择按钮时显示所选图像。
怎么做?
解决方案
您可以使用以下代码显示默认和选定的按钮图像:
buttonType.setImage(UIImage(named: "defaultImage")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
buttonType.setImage(UIImage(named: "selectedImage")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)
推荐阅读
- php - 获取当前产品类别所有产品父 sku 并以数组形式显示它们 - Woocommerce
- typescript - 为什么 TypeScript 编译器将函数包装在 (0, module.fn) 中?
- python - 逐元素添加 numpy 数组列表
- matlab - 如何将具有 16 位整数数据的 .dat 文件组转换为 .txt 文件组?
- c++ - 无法开始调试。意外的 GDB 输出
- java - Android ImageAnalysis - 每 n 帧分析一次
- c - 如何检查日期格式是否正确
- java - JSON 到 JSON 的转换:可能的方法
- javascript - Mongoose 中间件挂钩
- c++ - 修改红黑树的插入函数会出错