I made a checklist using UITableView, but it doesn't check when I press it.

Asked 2 years ago, Updated 2 years ago, 122 views

What do you want to do

I am making a list with a check box using UITableView in Xcode.
I want to change the image of the blank box and the checked box when cell is pressed, but it doesn't work.
Currently, a blank box appears, but when you press cell, the cell is selected and the image remains unchanged.Is there a solution?

Also, if possible, I would like to cancel the selection after pressing the cell.

If you understand, please help me.

Source Code

The data to be displayed in the list is retrieved from the dictionary commonDictionary found in the model file.

import UIKit

class ShoppingTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var shoppingTableView: UITableView!
    
    varval=commonDictionary [key2021323]
    lazy var shopList: [[String:String]] = val! [keyIngList] as! [[String:String]]
    var localDate="hoge"
    
    varchecked: UIImage = UIImage(named: "checked-checkbox --v1.png")!
    varunchecked: UIImage=UIImage(named: "unchecked-checkbox.png")!
    varud=UserDefaults.standard
    var status = String()
    
    func updateData(_selectedDate:String){
        localDate = selectedDate
        self.val=commonDictionary [selectedDate]
        if val!=nil{
            self.shopList=self.val![keyIngList] as![[String:String]]
        }
    }
    
    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        return shopList.count
    }
    
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        
        letcell:UITableViewCell=tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
        lettemp=shopList [indexPath.row]
        lettingname = temp [keyIngName]
        lettingq = temp [keyIngQ]
        ifingname!=nil,ingq!=nil{
            letlabelString=ingname!+":"+ingq!
            cell.textLabel!.text=labelString
            if(self.val![labelString]!=nil){
                cell.imageView?.image=UIImage(named: "checked-checkbox --v1.png")
            } else{
                cell.imageView?.image=UIImage(named: "unchecked-checkbox.png")
            }
        }
        return cell
    }
    
    functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
        iflet cell=tableView.cellForRow(at:indexPath){
            letcellText=cell.textLabel?.text
            ifcell.imageView?.image==UIImage(named: "checked-checkbox --v1.png") {
                self.val?.updateValue(false, forKey:cellText!)
                cell.imageView?.image=UIImage(named: "unchecked-checkbox.png")
            } else{
                self.val?.updateValue(true, forKey:cellText!)
                cell.imageView?.image=UIImage(named: "checked-checkbox --v1.png")
            }
            cell.isSelected=false
        }
    }
    
    functableView(_tableView:UITableView, heightForRowAtindexPath:IndexPath)->CGFloat{
        return56
    }
    
    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        
    }
}

Development Environment

Swift5
Mac OS 11.2.2
Xcode 12.4

swift xcode uitableview swift5

2022-09-29 22:14

1 Answers

As OOPer commented, delete was not connected.
Sorry for the simplicity.


2022-09-29 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.