【问题标题】:How to disable/enable notifications in tablewiew by using UISwitch in Swift如何在 Swift 中使用 UISwitch 在 tablewiew 中禁用/启用通知
【发布时间】:2021-04-06 07:59:57
【问题描述】:

我正在开发一个基于位置的提醒应用程序。我显示用户在表格视图上创建的所有提醒。我在每个单元格上也有 UISwitch。我希望 UISwitch 单独禁用/启用提醒,而不是所有通知。我想不通。

    
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])
            
        }
        
    }
}

【问题讨论】:

    标签: swift xcode uitableview apple-push-notifications uiswitch


    【解决方案1】:

    我认为您的代码无法正常工作,因为单元格被重用,并且对特定单元格的操作应该从单元格类而不是 ViewController 处理。将此代码移动到UITableViewCell 代码中。

    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])
            
        }
        
    }
    

    并在UITableViewCell 中添加一个新属性

    weak var switchViewTag: Int?
    

    修改你的cellForRowAt委托方法为

    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
    
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多