【发布时间】:2018-03-21 16:15:57
【问题描述】:
我有一个UICollectionView。
在UICollectionView 的每个UICollectionViewCell 中,我都有一个UITableView。
在UITableView 的每个UITableViewCell 中,我都有一个UIButton。
当点击UIButton或UITableViewCell时,我想改变UIButton的图像。
这是我的 UICollectionView:
这是我的代码:
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)
}
}
这是我的 UICollectionViewCell 和 UITableView:
SurveyQuestionsCollectionViewCell.swift(在 .xib 文件的后面)
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()
}
}
这是我的 TableViewCell
SurveyQuestionsTableViewCell.swift(在 .xib 文件的后面)
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)
}
}
}
}
这是我的输出:
每次点击任何 TableCell 时,我都想更改 radioButton 的图像。但形象并没有改变。我还标记了更改图像的代码已执行但图像没有更改。
【问题讨论】:
-
检查按钮的插座是否正确?
-
@iPatel 你很好。
-
使数据源处理检查和取消检查行为。 Make 是选定的 Bool 变量。并在索引路径的 cellfor 行上根据数据源的该变量设置图像。只需在 didSelect 方法上更改特定索引的该变量的值,然后重新加载表 vie。
标签: ios swift uitableview uibutton uicollectionview