HomeViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. //
  2. // HomeViewController.m
  3. // RedAnt ERP Suite
  4. //
  5. // Created by Ray on 14-5-23.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomeViewController.h"
  9. #import "RANetwork.h"
  10. #import "const.h"
  11. #import "HomeTableViewCellBanner.h"
  12. #import "DetailViewController.h"
  13. #import "MainViewController.h"
  14. #import "HomeTableViewCellButtonBanner.h"
  15. #import "WebViewController.h"
  16. #import "ImageUtils.h"
  17. #import "HomeTableViewCellSlide.h"
  18. //#import "MainViewController.h"
  19. @interface HomeViewController ()
  20. @end
  21. @implementation HomeViewController
  22. -(void) dealloc
  23. {
  24. NSLog(@"HOME VIEWCONTROLLER DEALLOC");
  25. }
  26. -(void) refresh_on_login
  27. {
  28. // do not refresh on login;
  29. }
  30. #ifndef RA_NOTIFICATION
  31. -(void) reload_container_getdata:(bool)update_data
  32. {
  33. [super reload_container_getdata:update_data];
  34. if(update_data)
  35. [ self LoadData];
  36. else
  37. [self.homeTable reloadData];
  38. }
  39. #endif
  40. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  41. {
  42. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  43. if (self) {
  44. // Custom initialization
  45. }
  46. return self;
  47. }
  48. - (void)viewWillAppear:(BOOL)animated
  49. {
  50. [super viewWillAppear:animated];
  51. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  52. if(!CGSizeEqualToSize(self.ori_size, [[UIScreen mainScreen] bounds].size))
  53. {
  54. [self.homeTable reloadData];
  55. }
  56. // self.content = self.bak_content;
  57. // [self reload_container_getdata:false];
  58. }
  59. -(void) viewWillDisappear:(BOOL)animated
  60. {
  61. self.ori_size=[[UIScreen mainScreen] bounds].size;
  62. // [[NSNotificationCenter defaultCenter] postNotificationName:CYCLESCROLL_STOPTIMMER object:nil];
  63. // self.bak_content = self.content;
  64. // self.content= nil;
  65. //// self.homeTable re
  66. // [self.homeTable reloadData];
  67. }
  68. -(void)manually_refresh
  69. {
  70. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  71. if(self.isrefreshing)
  72. {
  73. [reF endRefreshing];
  74. return;
  75. }
  76. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  77. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  78. // DebugLog(@"refresh!!!!!!!!");
  79. }
  80. -(void) LoadData
  81. {
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. if(self.isrefreshing)
  84. {
  85. return;
  86. }
  87. self.isrefreshing=true;
  88. DebugLog(@"HOMEVIEW BEGIN LOAD");
  89. self.mum.hidden = false;
  90. [self.mum startAnimating];
  91. self.mum.center = self.view.center;
  92. self.label_net_err.hidden=true;
  93. self.homeTable.hidden=true;
  94. [RANetwork request_home:0 customid:0 completionHandler:^(NSMutableDictionary *result) {
  95. [self.mum stopAnimating];
  96. NSMutableDictionary* data=result;
  97. self.content = data;
  98. [self.homeTable reloadData];
  99. self.homeTable.hidden=false;
  100. if([[data valueForKey:@"result"] intValue]==2)
  101. {
  102. DebugLog(@"HOMEVIEW JSON LOADED!");
  103. }
  104. else
  105. if([[data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  106. {
  107. self.label_net_err.hidden=false;
  108. }
  109. else
  110. {
  111. [RAUtils message_alert:[data valueForKey:@"err_msg"] title:@"Loading Home" controller:self] ;
  112. }
  113. self.isrefreshing=false;
  114. ;
  115. }];
  116. return;
  117. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  118. //
  119. //
  120. // //debug
  121. //// NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"home_template" ofType:@"json" ]];
  122. //// NSError *error=nil;
  123. //// NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  124. ////
  125. //// NSMutableDictionary* data=[jsobj mutableCopy];
  126. // NSMutableDictionary* data=[[RANetwork load_HomePage:0 customid:0] mutableCopy];
  127. //
  128. // dispatch_async(dispatch_get_main_queue(), ^{
  129. //
  130. //
  131. // [self.mum stopAnimating];
  132. //
  133. // self.content = data;
  134. // [self.homeTable reloadData];
  135. // self.homeTable.hidden=false;
  136. // if([[data valueForKey:@"result"] intValue]==2)
  137. // {
  138. // DebugLog(@"HOMEVIEW JSON LOADED!");
  139. //
  140. // //debug
  141. // // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"button_banner1" ofType:@"json" ]];
  142. // // NSError *error=nil;
  143. // // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  144. // // [data setObject:jsobj forKey:@"view_3" ];
  145. //
  146. // }
  147. // else
  148. // if([[data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  149. // {
  150. // self.label_net_err.hidden=false;
  151. // }
  152. // else
  153. // {
  154. // [RAUtils message_alert:[data valueForKey:@"err_msg"] title:@"Loading Home" controller:self] ;
  155. // }
  156. //
  157. // self.isrefreshing=false;
  158. //
  159. //
  160. //
  161. // });
  162. // });
  163. });
  164. }
  165. -(void) ReloadData
  166. {
  167. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  168. [reF endRefreshing];
  169. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  170. [self LoadData];
  171. }
  172. - (void)viewDidLoad
  173. {
  174. [super viewDidLoad];
  175. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  176. self.label_net_err.layer.borderWidth = 2.0;
  177. self.label_net_err.layer.cornerRadius=15;
  178. self.label_net_err.layer.masksToBounds=true;
  179. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  180. // tap.minimumPressDuration = 0.8; //定义按的时间
  181. [self.label_net_err addGestureRecognizer:tap];
  182. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  183. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  184. ref.tag = 200 ;
  185. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  186. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  187. // ref.hidden = true;
  188. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  189. [self.homeTable addSubview:ref];
  190. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame];
  191. treeView.delegate = self;
  192. treeView.dataSource = self;
  193. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  194. [treeView reloadData];
  195. // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
  196. [treeView setBackgroundColor:UIColorFromRGB(0xF7F7F7)];
  197. self.treeView = treeView;
  198. [self.treeContainer addSubview:treeView];
  199. // UIApplication * app = [UIApplication sharedApplication];
  200. }
  201. //- (void)viewWillAppear:(BOOL)animated
  202. //{
  203. // [super viewWillAppear:animated];
  204. //
  205. // CGRect rt1 = self.treeContainer.bounds;
  206. // CGRect rt2 = self.treeContainer.frame;
  207. //
  208. //
  209. //
  210. // self.treeView.frame = self.treeContainer.bounds;
  211. //}
  212. -(void)viewDidAppear:(BOOL)animated
  213. {
  214. [super viewDidAppear:animated];
  215. // CGRect rt = self.treeContainer.bounds;
  216. // CGRect rt1 = self.treeContainer.frame;
  217. self.treeView.frame = self.treeContainer.bounds;
  218. // if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
  219. // CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
  220. // float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
  221. // self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
  222. // self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
  223. // }
  224. // self.homeTable.contentSize = self
  225. // self.scroll.contentSize = self.contentsize;
  226. }
  227. - (void)didReceiveMemoryWarning
  228. {
  229. [super didReceiveMemoryWarning];
  230. // Dispose of any resources that can be recreated.
  231. }
  232. //- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  233. //{
  234. // DebugLog(@"!!!!!!!!!!!!!!!!!!!!!route!!!!!!!!!!!!!!!!!!!!!");
  235. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  236. // [self.homeTable reloadData];
  237. // // CGSize contentsize = self.scroll.contentSize;
  238. // // contentsize.height =330+2 * 230+29+20+20 +1 * 230+29+20+20 ;
  239. // // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  240. // // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  241. // // {
  242. // // contentsize.height+=250;
  243. // // }
  244. // //
  245. // // self.scroll.contentSize = contentsize;
  246. // // // CGRect frame = self.view.frame;
  247. // // // [self.carouselController.view removeFromSuperview];
  248. // // // wait(2000);
  249. // // // self.carouselController = [[FPCarouselNonXIBViewController alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 400)];
  250. // // // [self.view addSubview:self.carouselController.view];
  251. // // // int i = self.view.subviews.count;
  252. // // // int b = 0;
  253. // // // [self.carouselController updateFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
  254. //}
  255. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  256. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  257. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  258. // what ever you want to prepare
  259. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  260. [self.homeTable reloadData];
  261. }];
  262. }
  263. #pragma mark - Table view data source
  264. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
  265. {
  266. if([cell isKindOfClass:[HomeTableViewCellSlide class]])
  267. {
  268. HomeTableViewCellSlide* slidecell=(HomeTableViewCellSlide*)cell;
  269. [slidecell.imgScroll stopTimmer];
  270. }
  271. }
  272. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  273. {
  274. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  275. // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  276. // {
  277. // self.orientation = orientation;
  278. // [self.collectionview.collectionViewLayout invalidateLayout];
  279. // DebugLog(@"routed");
  280. // }
  281. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  282. NSString* type = [view_json valueForKeyPath:@"type"];
  283. if([type isEqualToString:@"slideview"])
  284. {
  285. // if(UIInterfaceOrientationIsPortrait(orientation))
  286. // return 400;
  287. // else
  288. // return 533;
  289. // int i_width = 981;
  290. // int i_height = 512;
  291. int i_width = [[view_json valueForKey:@"width"]intValue];
  292. int i_height = [[view_json valueForKey:@"height"]intValue];
  293. if(i_width<=0||i_height<=0)
  294. {
  295. i_width=981;
  296. i_height=512;
  297. }
  298. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  299. return ui_height;
  300. }
  301. else if([type isEqualToString:@"topicview"])
  302. {
  303. // int item_count =[[view_json valueForKey:@"item_count"] intValue];
  304. // if(UIInterfaceOrientationIsPortrait(orientation))
  305. // return item_count/4 * 230 +49;
  306. return 310;
  307. }
  308. else if([type isEqualToString:@"banner"])
  309. {
  310. // if(UIInterfaceOrientationIsPortrait(orientation))
  311. // return 327;//[[view_json valueForKey:@"height"] intValue];
  312. int i_width = 498;
  313. int i_height = 197;
  314. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  315. return ui_height;
  316. }
  317. else if([type isEqualToString:@"button_banner"])
  318. {
  319. int i_width = [[view_json valueForKey:@"width"]intValue];
  320. int i_height = [[view_json valueForKey:@"height"]intValue];
  321. float ui_height = (tableView.frame.size.width * i_height)/(float)i_width;
  322. return ui_height;
  323. }
  324. return 44;
  325. }
  326. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  327. {
  328. //-----debug!!
  329. // if(self.content !=nil)
  330. // return 2;
  331. return [[self.content valueForKeyPath:@"count"]intValue];
  332. }
  333. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  334. {
  335. return 1;
  336. }
  337. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  338. {
  339. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  340. // NSString* type = [view_json valueForKeyPath:@"type"];
  341. NSString* action = [view_json valueForKeyPath:@"action"];
  342. if([action isEqualToString:@"itemSearch"])
  343. {
  344. NSString* covertype = [view_json valueForKeyPath:@"covertype"];
  345. UIApplication * app = [UIApplication sharedApplication];
  346. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  347. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  348. [main_vc switchToItemSearch:covertype];
  349. }
  350. }
  351. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  352. {
  353. UITableViewCell *cell = nil;
  354. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  355. NSString* type = [view_json valueForKeyPath:@"type"];
  356. if([type isEqualToString:@"slideview"])
  357. {
  358. NSString *CellIdentifier = @"HomeTableViewCellSlide";
  359. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  360. HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell;
  361. [slidecell.imgScroll stopTimmer];
  362. [slidecell.imgScroll removeFromSuperview];
  363. // for(UIView* v in cell.contentView.subviews)
  364. // {
  365. // [v removeFromSuperview];
  366. //
  367. // }
  368. // int i_width = 981;
  369. // int i_height = 512;
  370. //如果模版有高度就用模版设置值,没有则用默认值。
  371. int i_width = [[view_json valueForKey:@"width"]intValue];
  372. int i_height = [[view_json valueForKey:@"height"]intValue];
  373. if(i_width<=0||i_height<=0)
  374. {
  375. i_width=981;
  376. i_height=512;
  377. }
  378. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  379. // HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell;
  380. // imgScroll.tag = 1394;
  381. //
  382. //
  383. // for (UIView *v in cell.contentView.subviews) {
  384. // if(v.tag==1394)
  385. // [v removeFromSuperview];
  386. // }
  387. CycleScrollView *imgScroll = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, ui_height) animationDuration:5];
  388. imgScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  389. int slide_item_count = [[view_json valueForKey:@"count"] intValue];
  390. //imgScroll.frame =CGRectMake(0, 5, width, 300);
  391. [imgScroll reset];
  392. // [imgScroll animationDuration:5];
  393. imgScroll.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
  394. for(int i = 0;i<slide_item_count;i++)
  395. {
  396. NSDictionary * slide_itemjson =[view_json objectForKey:[NSString stringWithFormat:@"item_%d",i] ];
  397. SliderPage * tempview = [[SliderPage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ui_height)];
  398. tempview.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  399. NSString* img_url = [slide_itemjson valueForKey:@"img"];
  400. tempview.contentMode = UIViewContentModeScaleToFill;
  401. tempview.img_url = img_url;
  402. // tempview.url =@"";
  403. // tempview.action_type = ;
  404. // tempview.params = ;
  405. [imgScroll addPage:tempview];
  406. }
  407. // return cell;
  408. // return cell;
  409. //Capturing strongly warring;
  410. // __block CycleScrollView *brimgScroll= imgScroll;
  411. // imgScroll.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
  412. //
  413. //
  414. //
  415. // SliderPage* page =brimgScroll.arr_Pages[pageIndex];
  416. // [page loadImage];
  417. //
  418. //
  419. //
  420. // return page ;
  421. //
  422. // // UIImageView * v= viewsArray[pageIndex];
  423. // // return viewsArray[pageIndex];
  424. // };
  425. // imgScroll.totalPagesCount = ^NSInteger(void){
  426. // return brimgScroll.arr_Pages.count; //viewsArray.count;
  427. // };
  428. [imgScroll setTotalPagesCount];
  429. imgScroll.TapActionBlock = ^(NSInteger pageIndex){
  430. DebugLog(@"点击了第%ld个",(long)pageIndex);
  431. NSDictionary * item_json =[view_json objectForKey:[NSString stringWithFormat:@"item_%ld",pageIndex] ];
  432. NSString* action = [item_json valueForKeyPath:@"action"];
  433. [RASingleton sharedInstance].homeClickedItemName = @"slide_view";
  434. // if([action isEqualToString:@"itemSearch"])
  435. // {
  436. // [Singleton sharedInstance].homeItemClick = YES;
  437. // NSString* covertype = [item_json valueForKeyPath:@"covertype"];
  438. //
  439. // UIApplication * app = [UIApplication sharedApplication];
  440. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  441. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  442. //
  443. // [main_vc switchToItemSearch:covertype];
  444. // }
  445. // else
  446. if([action isEqualToString:@"category"])
  447. {
  448. [RASingleton sharedInstance].homeItemClick = YES;
  449. NSString* cid = [item_json valueForKeyPath:@"category_id"];
  450. UIApplication * app = [UIApplication sharedApplication];
  451. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  452. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  453. [main_vc switchToCagegory:cid];
  454. }
  455. };
  456. //
  457. slidecell.imgScroll = imgScroll;
  458. // return cell;
  459. [cell.contentView addSubview:imgScroll];
  460. }
  461. else if([type isEqualToString:@"topicview"])
  462. {
  463. NSString *CellIdentifier = @"HomeTableViewCellTopic";
  464. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  465. HomeTableViewCellTopic * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  466. cell.itemdelegate = self;
  467. cell.related_data = view_json;
  468. [cell.topicCollectionView reloadData];
  469. NSString * topictitle =[view_json valueForKey:@"title"];
  470. cell.label_title.text =topictitle;
  471. cell.backgroundColor = [UIColor whiteColor];
  472. return cell;
  473. }
  474. else if([type isEqualToString:@"banner"])
  475. {
  476. NSString *CellIdentifier = @"HomeTableViewCellBanner";
  477. // cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  478. //
  479. // HomeTableViewCellBanner * bannercell=cell;
  480. HomeTableViewCellBanner * bannercell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];;
  481. NSString* img_url =[view_json valueForKeyPath:@"img"];
  482. NSString* file_name=[img_url lastPathComponent];
  483. NSData* img_data=[ImageUtils load_cached_img:file_name loadFrom:img_url size:bannercell.BannerImageView.frame.size allow_enlarge:false];
  484. if(img_data!=nil)
  485. {
  486. UIImage * img =[UIImage imageWithData:img_data];
  487. bannercell.BannerImageView.image = img;
  488. }
  489. else
  490. {
  491. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  492. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  493. dispatch_async(dispatch_get_main_queue(), ^{
  494. if(downloadimg_data!=nil)
  495. {
  496. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  497. UIImage * img =[UIImage imageWithData:downloadimg_data];
  498. bannercell.BannerImageView.image = img;
  499. }
  500. else
  501. bannercell.BannerImageView.image=[UIImage imageNamed:@"notfound_l"];
  502. });
  503. });
  504. }
  505. cell.backgroundColor = [UIColor whiteColor];
  506. }
  507. else if([type isEqualToString:@"button_banner"])
  508. {
  509. // CellIdentifier = @"CommonEditorCellEdit";
  510. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  511. // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  512. // cell.backgroundColor = [UIColor whiteColor];
  513. // return cell;
  514. int cell_width = [[view_json valueForKey:@"width"]intValue];
  515. int cell_height = [[view_json valueForKey:@"height"]intValue];
  516. float ui_height = (tableView.frame.size.width * cell_height)/(float)cell_width;
  517. float ui_width = tableView.frame.size.width ;
  518. NSString *CellIdentifier = @"HomeTableViewCellButtonBanner";
  519. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  520. // HomeTableViewCellButtonBanner * bannercell=(HomeTableViewCellButtonBanner*)cell;
  521. for(UIView* v in cell.contentView.subviews)
  522. {
  523. [v removeFromSuperview];
  524. }
  525. int count = [[view_json valueForKey:@"count"] intValue];
  526. for(int i_item = 0;i_item<count;i_item++)
  527. {
  528. NSDictionary* item_json = [view_json objectForKey:[NSString stringWithFormat:@"item_%d",i_item]];
  529. int x = [[item_json valueForKey:@"x"]intValue];
  530. int y = [[item_json valueForKey:@"y"]intValue];
  531. int i_width = [[item_json valueForKey:@"width"]intValue];
  532. int i_height = [[item_json valueForKey:@"height"]intValue];
  533. float f_width = i_width/(cell_width/ui_width);
  534. float f_htight = i_height/(cell_height/ui_height);
  535. float fx = x/(cell_width/ui_width);
  536. float fy = y/(cell_height/ui_height);
  537. UIButton* btn_item = [[UIButton alloc] initWithFrame:CGRectMake(fx, fy, f_width, f_htight)];
  538. long tag = indexPath.section*1000+ indexPath.row*100+i_item;
  539. btn_item.tag = tag;
  540. [btn_item addTarget:self action:@selector(ButtonBannerClick:) forControlEvents:UIControlEventTouchUpInside];
  541. [cell.contentView addSubview:btn_item];
  542. NSString* img_url =[item_json valueForKeyPath:@"img"];
  543. NSString* file_name=[img_url lastPathComponent];
  544. NSData* img_data=[ImageUtils load_cached_img:file_name loadFrom:img_url size:btn_item.frame.size allow_enlarge:false];
  545. if(img_data!=nil)
  546. {
  547. UIImage * img =[UIImage imageWithData:img_data];
  548. [btn_item setBackgroundImage:img forState:UIControlStateNormal];
  549. }
  550. else
  551. {
  552. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  553. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  554. dispatch_async(dispatch_get_main_queue(), ^{
  555. if(downloadimg_data!=nil)
  556. {
  557. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  558. UIImage * img =[UIImage imageWithData:downloadimg_data];
  559. [btn_item setBackgroundImage:img forState:UIControlStateNormal];
  560. }
  561. else
  562. [btn_item setBackgroundImage:[UIImage imageNamed:@"notfound_l"] forState:UIControlStateNormal];
  563. });
  564. });
  565. }
  566. }
  567. cell.backgroundColor = [UIColor whiteColor];
  568. }
  569. return cell;
  570. }
  571. -(void)ButtonBannerClick:(id)sender{
  572. //这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数
  573. int tag = (int)[sender tag];
  574. int section = tag/1000;
  575. int row = (tag-section*1000)/100;
  576. int idx =(tag-section*1000 - row*100);
  577. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)section ]];
  578. NSDictionary* item_json = [view_json objectForKey:[NSString stringWithFormat:@"item_%d",idx]];
  579. NSString* action = [item_json valueForKeyPath:@"action"];
  580. [RASingleton sharedInstance].homeClickedItemName = item_json[@"description"];
  581. if([action isEqualToString:@"itemSearch"])
  582. {
  583. [RASingleton sharedInstance].homeItemClick = YES;
  584. NSString* covertype = [item_json valueForKeyPath:@"covertype"];
  585. UIApplication * app = [UIApplication sharedApplication];
  586. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  587. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  588. [main_vc switchToItemSearch:covertype];
  589. }
  590. else if([action isEqualToString:@"category"])
  591. {
  592. [RASingleton sharedInstance].homeItemClick = YES;
  593. NSString* cid = [item_json valueForKeyPath:@"category_id"];
  594. UIApplication * app = [UIApplication sharedApplication];
  595. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  596. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  597. [main_vc switchToCagegory:cid];
  598. }
  599. else if([action isEqualToString:@"webview"])
  600. {
  601. NSString* url = [item_json valueForKeyPath:@"url"];
  602. NSString* title = [item_json valueForKeyPath:@"title"];
  603. // UIApplication * app = [UIApplication sharedApplication];
  604. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  605. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  606. //
  607. //
  608. WebViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"WebViewController"];
  609. ViewController.url = url;
  610. ViewController.title = title;
  611. [self.navigationController pushViewController:ViewController animated:YES];
  612. }
  613. }
  614. #pragma mark TreeView Delegate methods
  615. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  616. {
  617. return 47;
  618. }
  619. - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  620. {
  621. return 3 * treeNodeInfo.treeDepthLevel;
  622. }
  623. - (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  624. {
  625. return YES;
  626. }
  627. - (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
  628. {
  629. if ([item isEqual:self.expanded]) {
  630. return YES;
  631. }
  632. return NO;
  633. }
  634. - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  635. {
  636. NSDictionary *data = item;
  637. int count = [[data valueForKey:@"count"] intValue];
  638. if(count==0)
  639. {
  640. // NSString* category = [data valueForKey:@"id"];
  641. // if (self.CateMenu_delegate && [self.CateMenu_delegate respondsToSelector:@selector(SelectCategory:)]) {
  642. // [self.CateMenu_delegate SelectCategory:category];
  643. // }
  644. // /* MainViewController* pvc = (MainViewController*)self.rootViewController;
  645. // [pvc switchToCagegory:category];*/
  646. // [self dismissViewControllerAnimated:YES
  647. // completion:^{
  648. // //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  649. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  650. // [app.window removeGestureRecognizer:self.tapGesture];
  651. // }];
  652. }
  653. // if (item == nil) {
  654. // NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%d",index] ];
  655. //
  656. // }
  657. // else
  658. // data=[data objectForKey:[NSString stringWithFormat:@"category_%d",index]];
  659. DebugLog(@"menu select %@",data);
  660. }
  661. - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  662. {
  663. if (treeNodeInfo.treeDepthLevel == 0) {
  664. cell.backgroundColor = UIColorFromRGB(0xF7F7F7);
  665. } else if (treeNodeInfo.treeDepthLevel == 1) {
  666. cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
  667. } else if (treeNodeInfo.treeDepthLevel == 2) {
  668. cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
  669. }
  670. }
  671. #pragma mark TreeView Data Source
  672. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  673. {
  674. // NSInteger numberOfChildren = [treeNodeInfo.children count];
  675. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  676. // if(numberOfChildren>0)
  677. // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]];
  678. cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"];
  679. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  680. if (treeNodeInfo.treeDepthLevel == 0) {
  681. cell.detailTextLabel.textColor = [UIColor blackColor];
  682. }
  683. return cell;
  684. }
  685. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  686. {
  687. if (item == nil) {
  688. UIApplication * app = [UIApplication sharedApplication];
  689. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  690. return [[appDelegate.categoryMenu valueForKey:@"count" ] intValue];
  691. // return [self.data count];
  692. }
  693. NSDictionary* data = item;
  694. return [[data valueForKey:@"count"] intValue];
  695. // RADataObject *data = item;
  696. // return [data.children count];
  697. }
  698. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  699. {
  700. NSDictionary *data = item;
  701. if (item == nil) {
  702. UIApplication * app = [UIApplication sharedApplication];
  703. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  704. NSDictionary * ret =[appDelegate.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ];
  705. return ret;
  706. }
  707. return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]];
  708. /*
  709. RADataObject *data = item;
  710. if (item == nil) {
  711. return [self.data objectAtIndex:index];
  712. }
  713. return [data.children objectAtIndex:index];
  714. */
  715. }
  716. #pragma mark - Topic cell delegate
  717. -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category
  718. {
  719. DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  720. // dvc
  721. dvc.product_id=product_id;
  722. dvc.category_id=category;
  723. dvc.ispush=true;
  724. [dvc reload];
  725. [self.navigationController pushViewController:dvc animated:true];
  726. }
  727. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  728. //{
  729. //
  730. // MainViewController* pvc = (MainViewController*)self.parentViewController;
  731. // // [pvc categoryMenuSelected:indexPath.row];
  732. //}
  733. - (void)reRefreshView {
  734. [self.homeTable reloadData];
  735. }
  736. #pragma mark - RA_NOTIFICAITON
  737. -(void) refresh_ui
  738. {
  739. [self.homeTable reloadData];
  740. }
  741. -(void) reload_data
  742. {
  743. [self LoadData];
  744. }
  745. @end