HomeViewController.m 32 KB

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