Swift Cell中观察者模式通知

2019-04-29  本文已影响0人  司南_01b7

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

 }

上一篇 下一篇

猜你喜欢

热点阅读