闭包:掌厨 † 实例三 ,闭包 + 属性
闭包的实现
class CookBookViewController: KTCHomeViewController 这是最底层
class CookBookViewController: KTCHomeViewController{
//滚动视图
var scrollView: UIScrollView?
//食材首页的推荐视图
private var recommendView: CBRecommendView?
//首页的食材视图
private var foodView: CBMaterialView?
//首页的分类视图
private var categoryView: CBMaterialView?
//导航的标题视图
private var segCtrl: KTCSegmentCtrl?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//导航
createMyNav()
//初始化视图
createHomePageView()
//下载推荐的数据
downloadRecommendData()
//下载食材的数据
downloadFoodData()
//下载分类的数据
downloadCategoryData()
}
//下载分类的数据
func downloadCategoryData() {
//参数
let params = ["methodName":"CategoryIndex"]
let downloader = KTCDownloader()
downloader.delegate = self
downloader.type = .Category
downloader.postWithUrl(kHostUrl, params: params)
}
//下载食材的数据
func downloadFoodData() {
//参数
let dict = ["methodName":"MaterialSubtype"]
let downloader = KTCDownloader()
downloader.delegate = self
downloader.type = .FoodMaterial
downloader.postWithUrl(kHostUrl, params: dict)
}
//初始化视图
func createHomePageView(){
self.automaticallyAdjustsScrollViewInsets = false
//1、创建一个滚动视图
scrollView = UIScrollView()
scrollView!.pagingEnabled = true
scrollView!.showsHorizontalScrollIndicator = false
//设置代理
scrollView?.delegate = self
view.addSubview(scrollView!)
//约束
scrollView!.snp_makeConstraints {
[weak self]
(make) in
make.edges.equalTo(self!.view).inset(UIEdgeInsetsMake(64, 0, 49, 0))
}
//2、创建容器视图
let containerView = UIView.createView()
scrollView!.addSubview(containerView)
//约束
containerView.snp_makeConstraints {
[weak self]
(make) in
make.edges.equalTo(self!.scrollView!)
make.height.equalTo(self!.scrollView!)
}
//3、添加子视图
//3.1、推荐
recommendView = CBRecommendView()
containerView.addSubview(recommendView!)
recommendView?.snp_makeConstraints(closure: {
(make) in
make.top.bottom.equalTo(containerView)
make.width.equalTo(kScreenWidth)
make.left.equalTo(containerView)
})
//3.2、食材
foodView = CBMaterialView()
foodView?.backgroundColor = UIColor.redColor()
containerView.addSubview(foodView!)
//约束
foodView?.snp_makeConstraints(closure: { (make) in
make.top.bottom.equalTo(containerView)
make.width.equalTo(kScreenWidth)
make.left.equalTo((recommendView?.snp_right)!)
})
//3.3、分类
categoryView = CBMaterialView()
categoryView?.backgroundColor = UIColor.purpleColor()
containerView.addSubview(categoryView!)
//约束
categoryView?.snp_makeConstraints(closure: { (make) in
make.top.bottom.equalTo(containerView)
make.width.equalTo(kScreenWidth)
make.left.equalTo((foodView?.snp_right)!)
})
//4、修改容器视图的大小
containerView.snp_makeConstraints { (make) in
make.right.equalTo(categoryView!)
}
}
//下载推荐的数据
func downloadRecommendData(){
//参数
let dict = ["methodName":"SceneHome"]
let downloader = KTCDownloader()
//设置代理
downloader.delegate = self
downloader.type = .Recommend
downloader.postWithUrl(kHostUrl, params: dict)
}
//创建导航
func createMyNav(){
//MSRK:- One
//标题位置
segCtrl = KTCSegmentCtrl(frame: CGRectMake(80, 0, kScreenWidth-80*2, 44), titleNames: ["推荐","食材","分类"])
//设置代理
segCtrl?.delegate = self
navigationItem.titleView = segCtrl
//扫一扫
addNavBtn("saoyisao", target: self, action: #selector(scanAction), isLeft: true)
//搜索
addNavBtn("search", target: self, action: #selector(searchAction), isLeft: false)
}
//扫一扫
func scanAction(){}
//搜索
func searchAction(){}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: 首页推荐部分的方法
//app://food_course_series#98#
//食材课程分集显示
func gotoFoodCoursePage(link: String){
//第一个#
let startRange = NSString(string: link).rangeOfString("#")
//第二个#
let endRange = NSString(string: link).rangeOfString("#", options: NSStringCompareOptions.BackwardsSearch, range: NSMakeRange(0, link.characters.count), locale: nil)
let id = NSString(string: link).substringWithRange(NSMakeRange(startRange.location+1, endRange.location-startRange.location-1))
//跳转界面
let foodCourseCtrl = FoodCourseViewController()
foodCourseCtrl.serialId = id
navigationController?.pushViewController(foodCourseCtrl, animated: true)
}
//显示首页推荐的数据
func showRecommendData(model: CBRecommendModel){
recommendView?.model = model
//点击事件
//MARK:- F 实现 final , 传到这里 就对了。
recommendView?.clickClosure = {
[weak self]
(title:String?, link: String) in
if link.hasPrefix("app://food_course_series") == true {
//app://food_course_series#98#
//食材课程分集显示
self!.gotoFoodCoursePage(link)
}
}
}