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