ebesalti
ebesalti

Reputation: 13

How to disable/enable notifications in tablewiew by using UISwitch in Swift

I am working on a location-based reminder app. I show all reminders that user created on a table view. I have also UISwitch on every cell. I want that UISwitch disables/enables reminders individually, not all notifications. I couldn't figure it out.

    
extension MainViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! itemTableViewCell
        
        let item = self.items![indexPath.row]
        cell.itemTitle?.text = item.itemName
        cell.itemSubTitle?.text = item.itemDescription
        
        
        
        //switch
            let swicthView = UISwitch(frame: .zero)
            swicthView.onTintColor = UIColor (named: "DingerBlue")
            swicthView.setOn(true, animated: true)
            swicthView.tag = indexPath.row
            swicthView.addTarget(self, action: #selector(self.SwitchBtn(_:)), for: .valueChanged)
            cell.accessoryView = swicthView
            let itemToRemove = self.items![indexPath.row]
            let notifToRemove: String = itemToRemove.notifID!
        
            return cell
        
    }
    @objc func switchDidChanged(_ sender: UISwitch){
        
      
        print("Switch value is \(sender.isOn)")

        if(sender.isOn){
            print("on")
            UIApplication.shared.registerForRemoteNotifications()
        }
        else{
            print("Off")
            UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notifToRemove])
            
        }
        
    }
}

Upvotes: 0

Views: 197

Answers (1)

Luca Sfragara
Luca Sfragara

Reputation: 634

I believe your code is not working because cells are reused and actions on specific cells should be handled from the cell class rather than from ViewController. Move this code into the UITableViewCell code.

let swicthView = UISwitch(frame: .zero)
swicthView.onTintColor = UIColor (named: "DingerBlue")
swicthView.setOn(true, animated: true)
swicthView.tag = switchViewTag!
swicthView.addTarget(self, action: #selector(self.SwitchBtn(_:)), for: .valueChanged)

 @objc func switchDidChanged(_ sender: UISwitch){
    
  
    print("Switch value is \(sender.isOn)")

    if(sender.isOn){
        print("on")
        UIApplication.shared.registerForRemoteNotifications()
    }
    else{
        print("Off")
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notifToRemove])
        
    }
    
}

and add a new property in the UITableViewCell

weak var switchViewTag: Int?

Modify your cellForRowAt delegate method to

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath)
    let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! itemTableViewCell
    
    let item = self.items![indexPath.row]
    cell.itemTitle?.text = item.itemName
    cell.itemSubTitle?.text = item.itemDescription
    cell.switchViewTag = indexPath.row

    return cell

    } 

Upvotes: 0

Related Questions