HomeViewController.m 36 KB

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