纯swift实现ipad版简单美团界面功能

2020-01-09 00:06:44王振洲

3 发送通知(分类模块)—-> 通知书写位置: 协议方法中

—-> 3.1 代码块一 :


//用户点击了左侧,告诉代理点击了左侧的哪一行
  func lrTableView(seletLeftButton leftRow: Int) {
    //从模型中取出数据
    let catrgoryData = categories[leftRow]
    //判断左侧是否有子数据
    let subCatroyData = catrgoryData.subcategories?.count
    //如果没有子数据,就将数据发送给外界,进行数据更改
    if subCatroyData == 0 {
      //通过通知的方式发送
      NSNotificationCenter.defaultCenter().postNotificationName(XFJCategoryNotification, object: nil, userInfo: [XFJCategoryNotificationKey : catrgoryData])
    }

  }

—-> 3.2 代码块二 :


//用户点击了右侧,高度代理点击了右侧哪一行,同时告诉代理选中了左侧哪一行
  func lrTableView(seletRightButton rightRow: Int, seletLeftButton leftRow: Int) {
    //从模型中获取数据
    let catrgoriesData = categories[leftRow]
    //取出子数据
    let subCatrgoriesData = catrgoriesData.subcategories![rightRow]
    //发送通知
    NSNotificationCenter.defaultCenter().postNotificationName(XFJCategoryNotification, object: nil, userInfo: [XFJCategoryNotificationKey : catrgoriesData, XFJSubCategoryNotificationKey : subCatrgoriesData])
  }

4 接收通知 : 虽然发送的通知是匿名通知,但是最好让能将数据提供给谁的一方接收通知,这样也方便设置相关数据

—-> 4.1 item是属于XFJHomeViewController类的,就让该类来接收通知,并实现通知中的方法

—-> 4.2 接收通知代码 :


//接收分类通知
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "categoriesNotic:", name: XFJCategoryNotification, object: nil)
    //接收地区通知
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "districtNotic:", name: XFJDistrictNotification, object: nil)
    //接收排序通知
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "sortsNotic:", name: XFJSortsNotification, object: nil)

—-> 4.3 移除通知 (重要点)


//移除通知
  deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
  }

5 实现接收通知中的方法

—-> 5.2 分类


///MARK : - 实现接收分类通知的中调用的方法
extension XFJHomeViewController {
    @objc private func categoriesNotic(nic : NSNotification) {
    //取出通知中的内容
    let catrgoryData = nic.userInfo![XFJCategoryNotificationKey] as! XFJCategories
    //此处(有可能没有子数据,所以这里不需强转)
    let subCatroyData = nic.userInfo![XFJSubCategoryNotificationKey]
    //设置数据(获取顶部的view)
    let categoryTopView = topItem?.customView as! XFJTopView
    //子数据
    let count = catrgoryData.subcategories?.count
    //判断
    if count == 0 {
      categoryTopView.title = "美团"
      categoryTopView.subtitle = catrgoryData.name
    }else{
      categoryTopView.title = catrgoryData.name
      categoryTopView.subtitle = subCatroyData as! String?
    }
    //设置图标
    categoryTopView.normalName = catrgoryData.icon
    categoryTopView.heightName = catrgoryData.highlighted_icon

    //退出poper
     categoryVC.dismissViewControllerAnimated(true) { () -> Void in
      //dismiss后允许交互
      self.setEnabled()
    }
  }
}