I have a UICollectionView
.
Inside each UICollectionViewCell
of UICollectionView
, I have a UITableView
.
Inside each UITableViewCell
of UITableView
, I have a UIButton
.
When clicking on UIButton
or UITableViewCell
, I want to change the image of UIButton
.
Here is my UICollectionView:
Here is my code:
SurveyQuestionsCollectionViewController.swift
import UIKit
class SurveyQuestionCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var survey_questions: [Dictionary<String, Any>] = []
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.register(UINib.init(nibName: "SurveyQuestionsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "survey_cell")
let layout = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
let collectionViewSize = self.collectionView?.frame.size
let itemWidth = Int((collectionViewSize?.width)!)
let itemHeight = Int((collectionViewSize?.height)!)
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
common()
//result is coming from API call which I have deleted as it is not necessary to describe my problem
self.survey_questions = result["survey_questions"] as! [Dictionary<String, Any>]
self.collectionView?.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
common()
return survey_questions.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var question = survey_questions[indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "survey_cell", for: indexPath) as! SurveyQuestionsCollectionViewCell
cell.setValues(question: question["question"]! as! String,
answers: question["answers"]! as! [Dictionary<String, Any>])
return cell
}
func common() {
self.collectionView.setContentOffset(CGPoint(x: CGFloat(currentPosition) * self.collectionView.frame.size.width, y: 0), animated: true)
}
}
Here is my UICollectionViewCell and UITableView:
SurveyQuestionsCollectionViewCell.swift (at the back of .xib file)
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()
}
}
Here is my TableViewCell
SurveyQuestionsTableViewCell.swift (at the back of .xib file)
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)
}
}
}
}
And here is my output:
Every time when I click on any TableCell, I would like to change the image of radioButton. But the image is not changing. I have also marked that code for changing image is executed but the image is not changing.