Swift UITableView按钮颜色问题
在Swift UITableView按钮颜色问题,swift,xcode,uitableview,Swift,Xcode,Uitableview,在UITableView中,我有一些问题需要回答是或不是,每个选项(是/否)都是单元格中自己的按钮 首先,我必须处理click事件,因为我必须在单击时更改按钮的backgroundColor,并且必须添加单击的行以创建另一个包含答案的结构 我的实际代码如下所示: extension MyViewController: UITableViewDelegate{ func tableView(_ tableView: UITableView, didSelectRowAt indexPath
UITableView
中,我有一些问题需要回答是或不是,每个选项(是/否)都是单元格中自己的按钮
首先,我必须处理click事件,因为我必须在单击时更改按钮的backgroundColor
,并且必须添加单击的行以创建另一个包含答案的结构
我的实际代码如下所示:
extension MyViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//click row
}
}
extension MyViewController: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellAnswers", for: indexPath) as! MyTableViewCell
//iterate here? and how can i do that?
cell.actionYes = {
cell.buttonYes.backgroundColor = .blue
cell.buttonNo.backgroundColor = .darkGray
}
cell.actionNot = {
cell.buttonYes.backgroundColor = .darkGray
cell.buttonNo.backgroundColor = .blue
}
let question = questions[indexPath.row]
cell.question = question
return cell
}
}
enum Answer {
case yes, no
}
struct QuestionResponse {
var answer: Answer?
}
正如你们所看到的,我有权处理点击事件并改变背景颜色,但如果我点击一个按钮,其余的也会改变
还有我的TableViewCell:
import UIKit
class MyTableViewCell: UITableViewCell{
@IBOutlet weak var question: UILabel!
@IBOutlet weak var buttonYes: UIButton!
@IBOutlet weak var buttonNo: UIButton!
var actionYes: (() -> Void)? = nil
var actionNo: (() -> Void)? = nil
@IBAction func answerYes(_ sender: Any) {
actionYes?()
}
@IBAction func answerNo(_ sender: Any) {
actionNo?()
}
var question: QuestionResponse!{
didSet{
updateUI()
}
}
func updateUI(){
question.text = question.value
}
}
我不知道如何迭代单元格以填充按钮上的正确颜色。您所面临的是重复使用单元格的结果,而UTableView就是这样做的 每当一个单元格不在屏幕上时,(滚动后)就会被添加到队列中以供重用。这就是为什么需要调用函数来获取一个单元格,以便为显示数据做好准备。这意味着,
UITableViewCell
实例可能已经有了上次使用时遗留下来的一些更改
处理UTableView
的正确方法是保留数据源中的任何更改。例如,您可以将answer
属性添加到QuestionResponse
模型中,如下所示:
extension MyViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//click row
}
}
extension MyViewController: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellAnswers", for: indexPath) as! MyTableViewCell
//iterate here? and how can i do that?
cell.actionYes = {
cell.buttonYes.backgroundColor = .blue
cell.buttonNo.backgroundColor = .darkGray
}
cell.actionNot = {
cell.buttonYes.backgroundColor = .darkGray
cell.buttonNo.backgroundColor = .blue
}
let question = questions[indexPath.row]
cell.question = question
return cell
}
}
enum Answer {
case yes, no
}
struct QuestionResponse {
var answer: Answer?
}
并将用户的答案存储在那里。此外,您还必须确保始终设置按钮的背景色,以避免以前使用同一单元格实例时产生不必要的剩余颜色:
extension MyViewController: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellAnswers", for: indexPath) as! MyTableViewCell
//iterate here? and how can i do that?
cell.actionYes = {
cell.buttonYes.backgroundColor = .blue
cell.buttonNo.backgroundColor = .darkGray
}
cell.actionNot = {
cell.buttonYes.backgroundColor = .darkGray
cell.buttonNo.backgroundColor = .blue
}
let question = questions[indexPath.row]
switch question.answer {
case .yes:
cell.buttonYes.backgroundColor = .blue
cell.buttonNo.backgroundColor = .darkGray
case .no:
cell.buttonYes.backgroundColor = .darkGray
cell.buttonNo.backgroundColor = .blue
default:
cell.buttonYes.backgroundColor = .darkGray
cell.buttonNo.backgroundColor = .darkGray
}
cell.question = question
return cell
}
}
从
didSelectRowAt
方法中的所选单元格中获取单元格indexPath
,然后使用tableView方法获取相应的单元格问题在于,当您点击“是”或“否”按钮并滚动时,您还会看到具有相同答案的其他问题?@gcharita yup。。例如,我在第一个问题上回答“是”,当我滚动时,其他问题在我尚未选择任何答案时标记为“是”。。我读到我必须在单元格中迭代,但我不知道怎么做