Ios 即使执行了更改图像的代码,按钮的图像也不会更改
我有一个UICollectionView 在UICollectionView的每个UICollectionViewCell中,我都有一个UITableView 在UITableView的每个UITableView单元格中,我都有一个UIButton 单击UIButton或UITableViewCell时,我想更改UIButton的图像 以下是我的UICollectionView: 这是我的密码: 调查问题CollectionViewController.swift 这是我的输出:Ios 即使执行了更改图像的代码,按钮的图像也不会更改,ios,swift,uitableview,uibutton,uicollectionview,Ios,Swift,Uitableview,Uibutton,Uicollectionview,我有一个UICollectionView 在UICollectionView的每个UICollectionViewCell中,我都有一个UITableView 在UITableView的每个UITableView单元格中,我都有一个UIButton 单击UIButton或UITableViewCell时,我想更改UIButton的图像 以下是我的UICollectionView: 这是我的密码: 调查问题CollectionViewController.swift 这是我的输出: 每次单击任何T
每次单击任何TableCell时,我都想更改radioButton的图像。但形象没有改变。我还标记了更改图像的代码已执行,但图像未更改。这是因为您设置了cell.SetRadioMageRowSelected:false,tableView:tableView代码IndidedSelectRowAtMethod查看下面的代码 只需删除最后一行 更新: 对于tableView\uTableView:UITableView中的get单元格,您需要在下面的行中写入didSelectRowAt indexPath:indexPath
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Your Stuff
let cell = tableView.cellForRow(at: indexPath) // Change this line
// Your Stuff
}
不要做那么多,按照下面的步骤做,简短明了 选择按钮并为其状态指定图像。 a。默认状态 b。对于所选状态 在调查问题集合视图单元格中 var indexSelected:Int=0 在cellForRowAt 在迪德罗瓦特
如果仍然面临任何问题,请询问。检查按钮的插座是否正确?@iPatel这很好。让Datasource处理检查和取消检查行为。Make是选定的布尔变量。在cellfor行的索引路径上,根据数据源的变量设置图像。只需在didSelect方法上更改特定索引变量的值,然后重新加载表vie。谢谢您的回答。我现在已经试过了,但输出仍然没有变化。在didSelectRow。。。方法使用let cell=self.tableView.cellforrowatinedexpathindexpath获取cell谢谢!现在效果很好。你能给我解释一下上面提到的行吗?如果你想用didSelectRowAtIndexPath方法得到选中的单元格,那么你可以通过上面的代码得到它?谢谢你的回答。我会尝试一下,学习一些新的东西@Vishal试图使您的代码整洁、干净、简单,以达到未来的目的。
import UIKit
class SurveyQuestionsCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {
var answers: [Dictionary<String, Any>] = []
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
override func layoutSubviews() {
super.layoutSubviews()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView?.register(UINib.init(nibName: "SurveyQuestionsTableViewCell", bundle: nil), forCellReuseIdentifier: "survey_answer_cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return answers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let answer = answers[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "survey_answer_cell", for: indexPath) as! SurveyQuestionsTableViewCell
cell.setRadioText(text: answer["answer"]! as! String)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "survey_answer_cell", for: indexPath) as! SurveyQuestionsTableViewCell
cell.setRadioImage(rowSelected: true, tableView: tableView)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "survey_answer_cell", for: indexPath) as! SurveyQuestionsTableViewCell
cell.setRadioImage(rowSelected: false, tableView: tableView)
}
func setValues(question: String, answers: [Dictionary<String, Any>]) {
self.questionLabel.text = question
self.answers = answers
self.tableView.reloadData()
}
}
import UIKit
class SurveyQuestionsTableViewCell: UITableViewCell {
@IBOutlet weak var radioButton: UIButton!
@IBOutlet weak var radioLabel: UILabel!
func setRadioText(text: String) {
radioLabel.text = text
}
func setRadioImage(rowSelected: Bool, tableView: UITableView) {
if rowSelected {
if let image = UIImage(named: "radioOn") as UIImage! {
self.radioButton.setImage(image, for: UIControlState.normal)
}
}
} else {
if let image = UIImage(named: "radioOff") as UIImage! {
self.radioButton.setImage(image, for: UIControlState.normal)
}
}
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "survey_answer_cell", for: indexPath) as! SurveyQuestionsTableViewCell
cell.setRadioImage(rowSelected: false, tableView: tableView)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Your Stuff
let cell = tableView.cellForRow(at: indexPath) // Change this line
// Your Stuff
}
if indexPath.row == indexSelected{
cell.button.isSelected = true
}
else{
cell.button.isSelected = false
}
self.indexSelected = indexPath.row
table.reloadData()