Swift tableviewcell在滚动时更改图像

Swift tableviewcell在滚动时更改图像,swift,image,button,scroll,tableview,Swift,Image,Button,Scroll,Tableview,我在tableview单元格中有四个按钮,每个按钮都有一个唯一的值,分别为0、1、2和3。 这四个按钮并链接到单个按钮的动作方法。 tableviewcell中的代码如下所示: @IBOutlet weak var questionLbl: UILabel! @IBOutlet weak var q1: UIButton! @IBOutlet weak var q2: UIButton! @IBOutlet weak var q3: UIButton!

我在tableview单元格中有四个按钮,每个按钮都有一个唯一的值,分别为0、1、2和3。 这四个按钮并链接到单个按钮的动作方法。 tableviewcell中的代码如下所示:

 @IBOutlet weak var questionLbl: UILabel!
  
     @IBOutlet weak var q1: UIButton!
     @IBOutlet weak var q2: UIButton!
     @IBOutlet weak var q3: UIButton!
     @IBOutlet weak var q4: UIButton!
    typealias emptyButtonClosure = (String,Int) -> ()
    var buttonTapped:emptyButtonClosure?
 @IBAction func btnClicked(sender:UIButton){

        let buttonArray = [q1,q2,q3,q4]
 
        for button in buttonArray {
           
            if sender.tag == button!.tag{
                
                button!.isSelected = true;
                if let buttonTapped = buttonTapped{
                    buttonTapped(sender.titleLabel!.text!,sender.tag)
                }
              
                if let image = UIImage(named: "checked-radio") {
                     button!.setImage(image, for: .normal)
                }
                else
                {
                    let image1 = UIImage(named: "unchecked-radio")
                    button!.setImage(image1, for: .normal)
                }
               
            }else{
               
                button!.isSelected = false;
               if let image = UIImage(named: "unchecked-radio") {
                     button!.setImage(image, for: .normal)
                }
               else
               {
                   let image1 = UIImage(named: "checked-radio")
                   button!.setImage(image1, for: .normal)
               }
            }
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        

        
        let cell = tableView.dequeueReusableCell(withIdentifier: "questioncell", for: indexPath) as! ChildQuestionCell
        

        cell.questionLbl.text = self.listData?.nanniBookingDetails.questionsData[indexPath.row].question

     cell.q1.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option1, for: .normal)
        cell.q2.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option2, for: .normal)
        cell.q3.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option3, for: .normal)
        cell.q4.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option4, for: .normal)
        
        cell.buttonTapped = { value,index in
            print("button selected is",value,index)
            self.answers.append(value)
        }

return cell
}
这些按钮被分组到称为buttonarray的单个数组中,因为这些按钮是单选按钮,只允许单个选择。 现在的问题是,tableview中的每个单元格都正确获取了值。但是当滚动tableview时,图像位置会不断变化。tableview中的代码如下所示:

 @IBOutlet weak var questionLbl: UILabel!
  
     @IBOutlet weak var q1: UIButton!
     @IBOutlet weak var q2: UIButton!
     @IBOutlet weak var q3: UIButton!
     @IBOutlet weak var q4: UIButton!
    typealias emptyButtonClosure = (String,Int) -> ()
    var buttonTapped:emptyButtonClosure?
 @IBAction func btnClicked(sender:UIButton){

        let buttonArray = [q1,q2,q3,q4]
 
        for button in buttonArray {
           
            if sender.tag == button!.tag{
                
                button!.isSelected = true;
                if let buttonTapped = buttonTapped{
                    buttonTapped(sender.titleLabel!.text!,sender.tag)
                }
              
                if let image = UIImage(named: "checked-radio") {
                     button!.setImage(image, for: .normal)
                }
                else
                {
                    let image1 = UIImage(named: "unchecked-radio")
                    button!.setImage(image1, for: .normal)
                }
               
            }else{
               
                button!.isSelected = false;
               if let image = UIImage(named: "unchecked-radio") {
                     button!.setImage(image, for: .normal)
                }
               else
               {
                   let image1 = UIImage(named: "checked-radio")
                   button!.setImage(image1, for: .normal)
               }
            }
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        

        
        let cell = tableView.dequeueReusableCell(withIdentifier: "questioncell", for: indexPath) as! ChildQuestionCell
        

        cell.questionLbl.text = self.listData?.nanniBookingDetails.questionsData[indexPath.row].question

     cell.q1.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option1, for: .normal)
        cell.q2.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option2, for: .normal)
        cell.q3.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option3, for: .normal)
        cell.q4.setTitle(self.listData?.nanniBookingDetails.questionsData[indexPath.row].option4, for: .normal)
        
        cell.buttonTapped = { value,index in
            print("button selected is",value,index)
            self.answers.append(value)
        }

return cell
}
如何在滚动图像时保持图像位置。例如,为第一行选择的图像不应显示在第三行或第四行中。它应仅保留在第一行中。
请告诉我如何解决这个问题?

单元格可以重复使用。你最好在数据模型中的某个地方维护单选按钮的状态。哪种数据模式?在
questionsData
中,这是来自APIYes的数据,但这是保存与UI相关的所有数据的最佳位置。