原代码
@implementation HomeViewSubController
- (void)loadBanner{
NSDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"5" forKey:@"type"];
__weak typeof(self) weakSelf = self;
[[NSNetworking sharedManager]get:@"/api/slider/list" parameters:parameters success:^(id response) {
BannerScrollModel *bannerScrollModel = [[BannerScrollModel alloc]initWithDictionary:response error:nil];
[weakSelf.bannerArray removeAllObjects];
[weakSelf.banners removeAllObjects];
[weakSelf.bannerArray addObjectsFromArray:bannerScrollModel.items];
for (NSInteger i = 0; i < bannerScrollModel.items.count; i++) {
BannerModel *model = bannerScrollModel.items[i];
[weakSelf.banners addObject:model.img_url];
}
if (weakSelf.bannerArray.count != 0) {
_homeHeaderView.bannerCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 225);
if (_model.ZN_activity.boolValue == YES) {
_homeHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 230+_homeHeaderView.earlyBirdView.height);
}else{
_homeHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 220+_homeHeaderView.earlyBirdView.height);
}
}else{
_homeHeaderView.bannerCollection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0);
_homeHeaderView.earlyBirdView.top = 5;
_homeHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,_homeHeaderView.earlyBirdView.height+0.01);
}
weakSelf.tableView.tableHeaderView = _homeHeaderView;
_homeHeaderView.bannerCollection.imagesArr = weakSelf.banners;
_homeHeaderView.bannerCollection.imagesCount = weakSelf.bannerArray.count;
} failure:^(NSString *error, int code) {
[MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
[WKProgressHUD popMessage:error inView:weakSelf.view duration:1.0 animated:YES];
}];
}
@end
JS代码
require('NSMutableDictionary,NSNetworking,BannerScrollModel,UIScreen,MBProgressHUD,WKProgressHUD');
defineClass('HomeViewSubController', {
loadBanner: function() {
var parameters = NSMutableDictionary.dictionary();
parameters.setValue_forKey("5", "type");
var weakSelf = __weak(self);
NSNetworking.sharedManager().get_parameters_success_failure("/api/slider/list", parameters, block('id', function(response) { var bannerScrollModel = BannerScrollModel.alloc().initWithDictionary_error(response, null); weakSelf.bannerArray().removeAllObjects(); weakSelf.banners().removeAllObjects();
weakSelf.bannerArray().addObjectsFromArray(bannerScrollModel.items());
for (var i = 0; i < bannerScrollModel.items().count(); i++) {
var model = bannerScrollModel.valueForKey("items").objectAtIndex(i);
weakSelf.banners().addObject(model.valueForKey("img_url"));
}
console.log(weakSelf.banners());
var _homeHeaderView = weakSelf.valueForKey("_homeHeaderView");
console.log(_homeHeaderView);
var screenBounds = UIScreen.mainScreen().bounds();
if (weakSelf.bannerArray().count() != 0) {
_homeHeaderView.bannerCollection().setFrame({x:0, y:0, width:screenBounds.width, height:225});
if (weakSelf.valueForKey("_model").valueForKey("ZN_activity").boolValue() == YES) {
_homeHeaderView.setFrame({x:0, y:0, width:screenBounds.width, height:(230 + _homeHeaderView.earlyBirdView().height())});
} else {
_homeHeaderView.setFrame({x:0, y:0, width:screenBounds.width, height:(220 + _homeHeaderView.earlyBirdView().height())});
}
} else {
_homeHeaderView.bannerCollection().setFrame({x:0, y:0, width:screenBounds.width, height:0});
_homeHeaderView.earlyBirdView().setTop(5);
_homeHeaderView.setFrame({x:0, y:0, width:screenBounds.width, height:_homeHeaderView.earlyBirdView().height() + 0.01});
}
weakSelf.tableView().setTableHeaderView(_homeHeaderView);
_homeHeaderView.bannerCollection().setImagesArr(weakSelf.banners());
_homeHeaderView.bannerCollection().setImagesCount(weakSelf.bannerArray().count());
}), block('NSString*,int', function(error, code) {
MBProgressHUD.hideAllHUDsForView_animated(weakSelf.view(), YES);
WKProgressHUD.popMessage_inView_duration_animated(error, weakSelf.view(), 1.0, YES);
}));
},
});
注意:
1.block中的self要在block外面声明成:
_ _weak typeof(self) weakSelf = self;
JS写法:var weakSelf = __weak(self);
2.for in循环改成for循环
3.多处用到实例变量的时候可以先声明再使用
如:var _homeHeaderView = weakSelf.valueForKey("_homeHeaderView");
4.获取屏幕宽度的方法:UIScreen.mainScreen().bounds().width
5.CGRectMake(0,0,5,10)写成{x:0,y:0,width:5,height:10}