【发布时间】:2019-03-02 23:35:33
【问题描述】:
这是我的代码。
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-
按钮类型 - 1 然后在正常状态下应显示默认图像,并在选择按钮时显示所选图像。
怎么办?
【问题讨论】:
标签: ios swift uitableview uibutton