JSPatch下发笔记7

简介: JSPatch下发笔记7

oc代码:

- (void)setcellModel:(HomeTopicModelDetail *)homemodel indexPath:(NSInteger)index{
    [_mainImageView sd_setImageWithURL:homemodel.banner_app placeholderImage:nil andType:HeightType];
    self.indexPath = index;
    NSInteger scrollCount = homemodel.items.count < 5 ? homemodel.items.count : 5;
    HomeTableViewSubView * iconImageView;
    for (int i=0; i<5; i++) {
        iconImageView=[_scrollView viewWithTag:kScrollViewSubViewTag+i];
        iconImageView.hidden=YES;
    }
    for(int i = 0;i < scrollCount;i++){
        HomeTopicModelSecDetail *smodel = homemodel.items[i];
        iconImageView=[_scrollView viewWithTag:kScrollViewSubViewTag+i];
        iconImageView.frame=CGRectMake( i*(10+90.0/375.0*screenWidth), 2+2, 90.0/375.0*screenWidth, 90.0/375.0*screenWidth-10);
        iconImageView.iconImageView.frame=CGRectMake(10, 5, 90.0/375.0*screenWidth, 90.0/375.0*screenWidth);
        [iconImageView.iconImageView sd_setImageWithURL:smodel.banner_url placeholderImage:nil andType:LowType];
        iconImageView.hidden=NO;
        __weak typeof(self)weakself=self;
        [iconImageView setTapActionWithBlock:^{
            [weakself.delegate NeiShaDetailjump:smodel.desId];
        }];
        iconImageView.summaryLabel.frame = CGRectMake(10, CGRectGetMaxY(iconImageView.iconImageView.frame)+6, iconImageView.frame.size.width, 16);
        iconImageView.summaryLabel.textColor=TEXT_COLOR1;
        iconImageView.summaryLabel.text = smodel.name;
        iconImageView.summaryLabel.font =  [UIFont fontWithName:@"PingFangSC" size:12.0f];
        iconImageView.priceLabel.frame = CGRectMake(10, CGRectGetMaxY(iconImageView.summaryLabel.frame)+2, iconImageView.summaryLabel.frame.size.width/2.0, 16);
        iconImageView.priceLabel.text = [NSString stringWithFormat:@"¥%@/天",smodel.money.formatFloat];
        iconImageView.priceLabel.font =  [UIFont fontWithName:@"PingFangSC" size:10.0f];
        iconImageView.priceLabel.textColor = [UIColor colorWithHexString:@"#f72222"];
        iconImageView.priceLabel.textAlignment = NSTextAlignmentLeft;
        iconImageView.costLabel.frame = CGRectMake(CGRectGetMaxX(iconImageView.priceLabel.frame), iconImageView.priceLabel.frame.origin.y, iconImageView.priceLabel.frame.size.width, iconImageView.priceLabel.frame.size.height);
        iconImageView.costLabel.text = [NSString stringWithFormat:@"¥%@",smodel.pri_price.formatFloat];
        iconImageView.costLabel.textAlignment = NSTextAlignmentRight;
        iconImageView.costLabel.font =  [UIFont fontWithName:@"PingFangSC" size:10.0f];
        iconImageView.costLabel.textColor = TEXT_COLOR1;
        [iconImageView.costLabel sizeToFit];
        iconImageView.costLabel.centerY=iconImageView.priceLabel.centerY;
        iconImageView.costLabel.right=iconImageView.width+10;
        if (smodel.pri_price.intValue<=0||smodel.pri_price.intValue==smodel.relet_money.intValue) {
            iconImageView.costLabel.hidden=YES;
        }else{
            iconImageView.costLabel.hidden=NO;
        }
        if (iconImageView.costLabel.hidden) {
            iconImageView.priceLabel.width=iconImageView.width;
        }
    }
    _scrollView.contentSize = CGSizeMake(iconImageView.right+20, CGRectGetHeight(_scrollView.frame));
    _scrollView.contentOffset=CGPointZero;
}


JS代码:

defineClass('HomeTableViewCell', {
            setcellModel_indexPath: function(homemodel, index) {
            var _mainImageView = self.valueForKey("_mainImageView");
            var _scrollView = self.valueForKey("_scrollView");
            _mainImageView.sd__setImageWithURL_placeholderImage_andType(homemodel.banner__app(), null, 1);
            self.setIndexPath(index);
            var scrollCount = homemodel.items().count() < 5 ? homemodel.items().count() : 5;
            var iconImageView;
            for (var i = 0; i < 5; i++) {
            iconImageView = _scrollView.viewWithTag(10000 + i);
            iconImageView.setHidden(YES);
            }
            for (var i = 0; i < scrollCount; i++) {
          (function(i){
            var smodel = homemodel.items().objectAtIndex(i);
            iconImageView = _scrollView.viewWithTag(10000 + i);
            var screenBounds = UIScreen.mainScreen().bounds();
            iconImageView.setFrame({x:i * (10 + 90.0 / 375.0 * screenBounds.width),y:2 + 2,width:90.0 / 375.0 * screenBounds.width,height:90.0 / 375.0 * screenBounds.width - 10});
            iconImageView.iconImageView().setFrame({x:10,y:5,width:90.0 / 375.0 * screenBounds.width,height:90.0 / 375.0 * screenBounds.width});
            iconImageView.iconImageView().sd__setImageWithURL_placeholderImage_andType(smodel.banner__url(), null, 3);
            iconImageView.setHidden(NO);
            var weakself = __weak(self);
            var desId = smodel.desId();
            iconImageView.setTapActionWithBlock(block(function() {
                weakself.delegate().NeiShaDetailjump(desId);
            }));
            iconImageView.summaryLabel().setFrame({x:10,y:iconImageView.iconImageView().frame().y+iconImageView.iconImageView().bounds().height + 6,width:iconImageView.bounds().width,height:16});
            iconImageView.summaryLabel().setTextColor(UIColor.colorWithHexString("#787878"));
            iconImageView.summaryLabel().setText(smodel.name());
            iconImageView.summaryLabel().setFont(UIFont.fontWithName_size("PingFangSC", 12));
            iconImageView.priceLabel().setFrame({x:10,y: iconImageView.summaryLabel().frame().y + iconImageView.summaryLabel().bounds().height + 2,width:iconImageView.summaryLabel().bounds().width / 2.0,height:16});
            iconImageView.priceLabel().setText(NSString.stringWithFormat("¥%@/天", smodel.money().formatFloat()));
            iconImageView.priceLabel().setFont(UIFont.fontWithName_size("PingFangSC", 10));
            iconImageView.priceLabel().setTextColor(UIColor.colorWithHexString("#f72222"));
            iconImageView.priceLabel().setTextAlignment(0);
            iconImageView.costLabel().setFrame({x:iconImageView.priceLabel().frame().x+iconImageView.priceLabel().bounds().width,y: iconImageView.priceLabel().frame().y,width:iconImageView.priceLabel().bounds().width,height:iconImageView.priceLabel().bounds().height});
            iconImageView.costLabel().setText(NSString.stringWithFormat("¥%@", smodel.pri__price().formatFloat()));
            iconImageView.costLabel().setTextAlignment(2);
            iconImageView.costLabel().setFont(UIFont.fontWithName_size("PingFangSC", 10));
            iconImageView.costLabel().setTextColor(UIColor.colorWithHexString("#787878"));
            iconImageView.costLabel().sizeToFit();
            iconImageView.costLabel().setCenterY(iconImageView.priceLabel().centerY());
            iconImageView.costLabel().setRight(iconImageView.width() + 10);
            if (smodel.pri__price().intValue() <= 0 || smodel.pri__price().intValue() == smodel.relet__money().intValue()) {
            iconImageView.costLabel().setHidden(1);
            } else {
            iconImageView.costLabel().setHidden(0);
            }
            if ( iconImageView.costLabel().hidden) {
            iconImageView.priceLabel().setWidth(iconImageView.width());
            }
             })(i)
            }
            _scrollView.setContentSize({width:iconImageView.right() + 20,height:_scrollView.bounds().height});
            _scrollView.setContentOffset({x:0,y:0});
            },
            });


总结:



当在for循环中用到block的时候,在JS中,block里取到的永远是i的最大值,这是由JS的闭包特性引起的,所谓闭包是指有权访问另一个函数作用域中的变量的函数,创建闭包的常见的方式就是,在一个函数内部创建另一个函数,通过另一个函数访问这个函数的局部变量。


在for循环里面的匿名函数执行 return i 语句的时候,由于匿名函数里面没有i这个变量,所以这个i他要从父级函数中寻找i,而父级函数中的i在for循环中,当找到这个i的时候,是for循环完毕的i,也就是i的最大值。

解决方案:

http://www.cnblogs.com/ZinCode/p/5551907.html


  1. 在JSPatch中OC所有CG相关的函数都不识别,如CGRectGetMaxY(iconImageView.iconImageView.frame),我们要想想CGRectGetMaxY函数的本意是什么,就是取控件的y坐标加上控件本身的高度。所以,在JS中可以写成:iconImageView.iconImageView().frame().y+iconImageView.iconImageView().bounds().height


3.JS中控件坐标的写法:xxx.frame().x,xxx.frame().y
xxx.bounds().width,xxx.bounds().height


4.JS中浮点数不能加f


5.CGSizeMake写成{width:xxx,height:xxx}


6.CGPointZero写成{x:0,y:0}


7.if判断条件里不能写函数,要改成变量,如 if ( iconImageView.costLabel().hidden)

目录
相关文章
|
安全
公网IP和私网IP
公网IP和私网IP
955 1
|
算法 JavaScript Java
使用强大的离线IP地址定位库ip2region获取城市信息
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java、php、c、python、nodejs、golang、c#等查询绑定和Binary,B树,内存三种查询算法。
使用强大的离线IP地址定位库ip2region获取城市信息
|
11月前
|
存储 NoSQL 关系型数据库
数据库管理系统
【10月更文挑战第8天】
504 1
|
12月前
|
开发工具 Android开发 git
Android实战之组件化中如何进行版本控制和依赖管理
本文介绍了 Git Submodules 的功能及其在组件化开发中的应用。Submodules 允许将一个 Git 仓库作为另一个仓库的子目录,有助于保持模块独立、代码重用和版本控制。虽然存在一些缺点,如增加复杂性和初始化时间,但通过最佳实践可以有效利用其优势。
152 3
|
JavaScript 前端开发
vue实现登录界面
vue实现登录界面
311 0
|
机器学习/深度学习 算法 Linux
xenomai内核解析--实时内存管理--xnheap
Xenomai是一个实时操作系统(RTOS)层,用于Linux,旨在提供确定性的任务调度和服务。其内存管理机制包括一个名为xnheap的内存池,确保内存分配和释放的时间确定性,以满足硬实时系统的严格需求。
355 0
xenomai内核解析--实时内存管理--xnheap
|
机器学习/深度学习 传感器 人工智能
探索现代技术中的人工智能:从理论到实践
本文旨在为读者提供一个关于人工智能的全面概述,包括它的定义、历史发展、当前应用和面临的挑战。我们将深入探讨机器学习和深度学习的基本概念,并通过实际案例分析,展示人工智能如何在不同领域发挥作用。此外,文章还将讨论人工智能对社会的影响,以及我们如何应对未来可能出现的变化。通过这篇概述,读者将获得对人工智能领域的初步理解,并激发进一步探索的兴趣。
|
机器学习/深度学习 人工智能 自然语言处理
软考之专家系统的概念
软考之专家系统的概念
236 0
|
JavaScript 前端开发 测试技术
关于项目中是否使用Typescript的疑惑与解答
关于项目中是否使用Typescript的疑惑与解答
239 0
YI
|
缓存 JavaScript
Vue学习笔记|Vue基础05
Vue学习笔记|Vue基础05
YI
79 1