美文网首页
Swift Cell中观察者模式通知

Swift Cell中观察者模式通知

作者: 司南_01b7 | 来源:发表于2019-04-29 15:55 被阅读0次

    1.贴上标签

    cell?.dingdanCancel.tag = indexPath.row

    2.viewDidLoad处定义通知

    let center = NotificationCenter.default

    center.addObserver(self, selector: #selector(notices), name: NSNotification.Name(rawValue: "CancelNotification"), object: nil)

    3.通知引发的事件

      @objc func notices(notification: Notification) {

            let userObj = notification.object as? Dictionary<String, Int>

            let index = userObj!["tag"]

            print("删除了:\(self.dingdanList[index!].hid)")

            self.cancel(by: index!)   

       }

    4.继发事件

    //删除cell方法

     @objc func cancel(by index: Int){

            dingdanList.remove(at: index)

            dingdanTableView.reloadData()

       }

    5.调用通知

    @IBAction func cancel(_ sender: UIButton) {

            let center = NotificationCenter.default

    //        center.post(name: NSNotification.Name(rawValue: "CancelNotification"), object: nil)

            center.post(name: NSNotification.Name(rawValue: "CancelNotification"), object: ["tag": sender.tag])

     }

    相关文章

      网友评论

          本文标题:Swift Cell中观察者模式通知

          本文链接:https://www.haomeiwen.com/subject/fbnynqtx.html