闭包:掌厨 Î 实例二 ,闭包 + 属性 ,各种 传值
响应链的 运用。 闭包的 传递, 依据 响应链
应该是 三层 layer
CBRecommendView, 这是中间层
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828c}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4}span.s1 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #1d9421}span.s8 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #1d9421}span.s9 {font-variant-ligatures: no-common-ligatures; color: #539aa4}span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s11 {font-variant-ligatures: no-common-ligatures; color: #294c50}span.s12 {font-variant-ligatures: no-common-ligatures; color: #3c828c}span.s13 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s14 {font-variant-ligatures: no-common-ligatures; color: #31595d}
class CBRecommendView: UIView {
//点击事件
//MARK:- 还有 这里。 1 D 闭包 的 传递
var clickClosure: ((String?, String) -> Void)?
//表格
private var tbView: UITableView?
//显示数据
var model: CBRecommendModel? {
didSet {
//刷新表格
tbView?.reloadData()
}
}
init() {
super.init(frame: CGRectZero)
//创建表格视图
tbView = UITableView(frame: CGRectZero, style: .Plain)
tbView?.delegate = self
tbView?.dataSource = self
//去掉分割线
tbView?.separatorStyle = .None
addSubview(tbView!)
//约束
tbView?.snp_makeConstraints(closure: {
[weak self]
(make) in
make.edges.equalTo(self!)
})
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//MARK: UITableView代理
extension CBRecommendView: UITableViewDelegate, UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//广告数据显示一个分组
var sectionNum = 1
if model?.data?.widgetList?.count > 0 {
sectionNum += (model?.data?.widgetList?.count)!
}
return sectionNum
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowNum = 0
if section == 0 {
//广告的数据
if model?.data?.banner?.count > 0 {
rowNum = 1
}
}else{
//其他的情况
let listModel = model?.data?.widgetList![section-1]
if listModel?.widget_type?.integerValue == WidgetType.GuessYourLike.rawValue {
//猜你喜欢
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.RedPackage.rawValue {
//红包入口
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.NewProduct.rawValue {
//今日新品
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.Special.rawValue {
//早餐日记、健康100岁等
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.Scene.rawValue {
//全部场景
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.Talent.rawValue {
//推荐达人
rowNum = (listModel?.widget_data?.count)! / 4
}else if listModel?.widget_type?.integerValue == WidgetType.Works.rawValue {
//精选作品
rowNum = 1
}else if listModel?.widget_type?.integerValue == WidgetType.Subject.rawValue {
//专题
rowNum = (listModel?.widget_data?.count)!/3
}
}
return rowNum
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var height: CGFloat = 0
if indexPath.section == 0 {
//广告的高度
if model?.data?.banner?.count > 0 {
height = 160
}
}else{
//其他情况
let listModel = model?.data?.widgetList![indexPath.section-1]
if listModel?.widget_type?.integerValue == WidgetType.GuessYourLike.rawValue {
//猜你喜欢
height = 80
}else if listModel?.widget_type?.integerValue == WidgetType.RedPackage.rawValue {
//红包入口
height = 100
}else if listModel?.widget_type?.integerValue == WidgetType.NewProduct.rawValue {
//今日新品
height = 300
}else if listModel?.widget_type?.integerValue == WidgetType.Special.rawValue {
//早餐日记、健康100岁等
height = 200
}else if listModel?.widget_type?.integerValue == WidgetType.Scene.rawValue {
//全部场景
height = 60
}else if listModel?.widget_type?.integerValue == WidgetType.Talent.rawValue {
//推荐达人
height = 80
}else if listModel?.widget_type?.integerValue == WidgetType.Works.rawValue {
//精选作品
height = 240
}else if listModel?.widget_type?.integerValue == WidgetType.Subject.rawValue {
//专题
height = 180
}
}
return height
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if indexPath.section == 0 {
//广告
if model?.data?.banner?.count > 0 {
cell = CBRecommendADCell.createAdCellFor(tableView, atIndexPath: indexPath, withModel: model!, cellClosure: clickClosure)
//MARK: - 好 4 E 闭包的 传递
}
}else{
//其他情况
let listModel = model?.data?.widgetList![indexPath.section-1]
if listModel?.widget_type?.integerValue == WidgetType.GuessYourLike.rawValue {
//猜你喜欢
cell = CBRecommendLikeCell.createLikeCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.RedPackage.rawValue {
//红包入口
cell = CBRedPacketCell.createRedPackageCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.NewProduct.rawValue {
//今日新品
cell = CBRecommendNewCell.createNewCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.Special.rawValue {
//早餐日记、健康100岁
cell = CBSpecialCell.createSpecialCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.Scene.rawValue {
//全部场景
cell = CBSceneCell.createSceneCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.Talent.rawValue {
//推荐达人
cell = CBTalentCell.createTalentCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.Works.rawValue {
//精选作品
cell = CBWorksCell.createWorksCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}else if listModel?.widget_type?.integerValue == WidgetType.Subject.rawValue {
//专题
cell = CBSubjectCell.createSubjectCellFor(tableView, atIndexPath: indexPath, withListModel: listModel!)
}
}
return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headView: UIView? = nil
if section > 0 {
//其他情况
let listModel = model?.data?.widgetList![section-1]
if listModel?.widget_type?.integerValue == WidgetType.GuessYourLike.rawValue {
//猜你喜欢
headView = CBSearchHeaderView(frame: CGRectMake(0,0,kScreenWidth,44))
}else if listModel?.widget_type?.integerValue == WidgetType.NewProduct.rawValue || listModel?.widget_type?.integerValue == WidgetType.Special.rawValue || listModel?.widget_type?.integerValue == WidgetType.Talent.rawValue || listModel?.widget_type?.integerValue == WidgetType.Works.rawValue || listModel?.widget_type?.integerValue == WidgetType.Subject.rawValue{
//今日新品 --> NewProduct
//早餐日记、健康100岁 -> Special
//推荐达人 --> Talent
//精选作品 --> Works
//专题 --> Subject
let tmpView = CBHeaderView(frame: CGRectMake(0,0,kScreenWidth,44))
tmpView.configTitle((listModel?.title)!)
headView = tmpView
}
}
return headView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var height: CGFloat = 0
if section > 0 {
//其他情况
let listModel = model?.data?.widgetList![section-1]
if (listModel?.widget_type?.integerValue == WidgetType.GuessYourLike.rawValue) || (listModel?.widget_type?.integerValue == WidgetType.NewProduct.rawValue) || (listModel?.widget_type?.integerValue == WidgetType.Special.rawValue) || (listModel?.widget_type?.integerValue == WidgetType.Talent.rawValue || (listModel?.widget_type?.integerValue == WidgetType.Works.rawValue) || (listModel?.widget_type?.integerValue == WidgetType.Subject.rawValue)) {
//猜你喜欢 -- GuessYourLike
//今日新品 -- NewProduct
//早餐日记、健康100岁 -- Special
//推荐达人 -- Talent
//精选作品 -- Works
//专题 -- Subject
height = 44
}
}
return height
}
//去掉粘滞性
func scrollViewDidScroll(scrollView: UIScrollView) {
let h: CGFloat = 44
if scrollView.contentOffset.y > h {
scrollView.contentInset = UIEdgeInsetsMake(-h, 0, 0, 0)
}else if scrollView.contentOffset.y > 0 {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0)
}
}
}