【问题标题】:Button's Image is not changing even though the code for changing image is executed即使执行了更改图像的代码,按钮的图像也不会改变
【发布时间】: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


【解决方案1】:

这是因为你设置了cell.setRadioImage(rowSelected: false, tableView: tableView) code indidDeselectRowAtmethod look at your below code

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)

}

只需删除最后一行。

更新:

要在tableView(_ tableView: UITableView, 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 
}

【讨论】:

  • 感谢您的回答。我现在已经尝试过了,但输出仍然没有变化。
  • 在 didSelectRow... 方法中使用 let cell = self.tableView.cellForRowAtIndexPath(indexPath) 获取单元格
  • 谢谢你!现在工作正常。你能解释一下上面提到的那一行吗?
  • 如果你想在 didSelectRowAtIndexPath 方法中获取选中的单元格,那么你可以通过上面的代码得到它。
  • Jay Girnari 正确的 junagadh?阿普杜塔拉拉吉尔
【解决方案2】:

不要做那么多,而是按照下面的步骤,简明扼要地做。

  1. 选择您的按钮并为其状态分配图像。

一个。默认状态

b.对于选定的州

  1. 在SurveyQuestionsCollectionViewCell

    var indexSelected : Int = 0

  2. 在cellForRowAt

      if indexPath.row == indexSelected{
             cell.button.isSelected = true
      }
      else{
             cell.button.isSelected = false
     }
    
  3. 在didSelectRowAt

      self.indexSelected = indexPath.row
      table.reloadData()
    

如果仍然遇到任何问题,请询问。

【讨论】:

  • 感谢您的回答。我会尝试并学习新的东西!
  • @Vishal 尝试使您的代码整洁、干净和简单,以达到未来的目的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 2023-04-04
  • 2012-10-02
  • 1970-01-01
相关资源
最近更新 更多