HomeViewController.m 30 KB

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