ResultViewController.m 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. //
  2. // ViewController.m
  3. // phoneTest
  4. //
  5. // Created by Ray on 01/09/2017.
  6. //
  7. //
  8. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  9. #import "ResultViewController.h"
  10. #import "FullyShowViewController.h"
  11. #import "RANetwork.h"
  12. #import "RAUtils.h"
  13. #import "JLRefreshHeader.h"
  14. #import "JLRefreshFooter.h"
  15. //#import "OrderDetailViewController.h"
  16. //#import "KVViewController.h"
  17. #import <QuickLook/QuickLook.h>
  18. #import "config.h"
  19. #import "RAConvertor.h"
  20. //#import "RTLabel.h"
  21. static const int delta = 25;
  22. @interface ResultViewController () <JLRefreshDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate>
  23. @property (nonatomic,strong) QLPreviewController *quickLook;
  24. @property (nonatomic,copy) NSString *documentPath;
  25. @property (nonatomic,assign) NSInteger offset;
  26. @property (nonatomic,strong) NSDictionary *params;
  27. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *w_constraint;
  28. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *h_constraint;
  29. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *trail_constraint;
  30. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *lead_constraint;
  31. @end
  32. @implementation ResultViewController
  33. - (void)labelClick {
  34. NSLog(@"underlineLabel被点击了");
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // self.labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)];
  39. //
  40. // self.labelTap.numberOfTouchesRequired = 1; //手指数
  41. // self.labelTap.numberOfTapsRequired = 2; //tap次数
  42. //
  43. //
  44. //
  45. //
  46. // self.cellDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellDoubleTapAction:)];
  47. //
  48. // self.cellDoubleTap.numberOfTouchesRequired = 1; //手指数
  49. // self.cellDoubleTap.numberOfTapsRequired = 2; //tap次数
  50. // UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
  51. // action:nil];
  52. // singleFingerOne.numberOfTouchesRequired = 1; //手指数
  53. // singleFingerOne.numberOfTapsRequired = 1; //tap次数
  54. //
  55. //
  56. // singleFingerTwo.delegate = self;
  57. self.label = [MDHTMLLabel new];
  58. // NSString* Path = [[NSBundle mainBundle] pathForResource:@"layout.json" ofType:nil];
  59. //
  60. // NSData *data = [NSData dataWithContentsOfFile:Path];
  61. // self.content_layout = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  62. //
  63. //
  64. // Path = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil];
  65. //
  66. // data = [NSData dataWithContentsOfFile:Path];
  67. // self.content_data = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  68. // int width=[self.content_layout[@"header"][@"width"] intValue];
  69. // if(width<self.tableview.frame.size.width)
  70. // width=self.tableview.frame.size.width;
  71. //
  72. // CGRect frame = CGRectMake(self.tableview.frame.origin.x, self.tableview.frame.origin.y, width, self.tableview.frame.size.height);
  73. // self.tableview.frame=frame;
  74. // self.scrollview.contentSize=self.tableview.frame.size;
  75. // Do any additional setup after loading the view, typically from a nib.
  76. self.automaticallyAdjustsScrollViewInsets = NO;
  77. [self setupNavigationBar];
  78. [self setupTableRefreshView];
  79. [self loadContent];
  80. self.tableview.tableHeaderView = [self get_tableHeader];
  81. }
  82. -(void)cellDoubleTapAction:(UIGestureRecognizer*)gestureRecognizer
  83. {
  84. NSAssert(true, ([NSString stringWithFormat:@"%s cellDoubleTapAction not impl",object_getClassName([self class])]));
  85. }
  86. //{
  87. // NSLog(@"cell double tap");
  88. // UITableViewCell* cell = (UITableViewCell*)[gestureRecognizer view];
  89. // NSIndexPath* indexPath=[self.tableview indexPathForCell:cell];
  90. // //strongSelf.content_action
  91. // if(self.content_action!=nil)
  92. // {
  93. //
  94. // if(self.content_action.count==1)
  95. // {
  96. // NSDictionary* action =self.content_action.firstObject;
  97. // NSString * module = action[@"module"];
  98. // if([module isEqualToString:@"quick_look"])
  99. // {
  100. //
  101. // // +(NSString*)download_file : (NSMutableDictionary*) params url:(NSString*) url
  102. // NSMutableDictionary *params = action[@"params"];
  103. // NSString* URL =action[@"url"];
  104. // __weak typeof(self) weakSelf = self;
  105. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  106. //
  107. //
  108. // NSString *path = [RANetwork download_file:params url:URL];
  109. // weakSelf.documentPath = path;
  110. //
  111. // if (path) {
  112. // dispatch_async(dispatch_get_main_queue(), ^{
  113. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  114. // });
  115. // }
  116. //
  117. // });
  118. // }
  119. // else if([module isEqualToString:@"kv_detail"])
  120. // {
  121. // NSMutableDictionary *params = [NSMutableDictionary new];
  122. // params[@"query_id"] = self.params[@"query_id"];
  123. //
  124. // NSMutableDictionary *criteria =[NSMutableDictionary new];
  125. // for(NSString* key in [action[@"params"] allKeys] )
  126. // {
  127. // NSArray* item =self.content_data[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  128. // int idx =[action[@"params"][key] intValue];
  129. // criteria[key]=item[ idx];
  130. // }
  131. //
  132. //
  133. // params[@"criteria"]=[RAConvertor dict2string:criteria] ;
  134. //
  135. //
  136. //
  137. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Loading..." title:@"Please wait"];
  138. //
  139. //
  140. // __weak typeof(self) weakself = self;
  141. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  142. //
  143. //
  144. //
  145. // NSDictionary* json =[RANetwork kv_detail:params];
  146. //
  147. // dispatch_async(dispatch_get_main_queue(), ^{
  148. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  149. //
  150. //
  151. // if([[json valueForKey:@"result"] intValue]==2)
  152. // {
  153. // KVViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"KVViewController"];
  154. // vc.content = [json mutableCopy];
  155. // [weakself.navigationController pushViewController:vc animated:NO];
  156. //
  157. // // weakself.content_data_download = [weakself CopyDirty:weakself.content_data_download to:[editor_json mutableCopy]];//[editor_json mutableCopy];
  158. // //
  159. // // weakself.content_data_control = [weakself translate_json:weakself.content_data_download changed: weakself.changed_data];
  160. // //
  161. // //
  162. // // [weakself download_success];
  163. // // [weakself.editorTable reloadData];
  164. //
  165. // }
  166. // else
  167. // {
  168. // [RAUtils message_alert:json[@"msg"] title:nil controller:weakself] ;
  169. // }
  170. //
  171. //
  172. //
  173. //
  174. // });
  175. // });
  176. // }
  177. // else if([module isEqualToString:@"order_detail"])
  178. // {
  179. //
  180. // OrderDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
  181. //
  182. //
  183. // vc.url_type = URL_REMOTE;
  184. // vc.request_url=URL_ORDER_DETAIL;
  185. //
  186. //
  187. // vc.params = [NSMutableDictionary dictionary];
  188. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"search_ui.json" ofType:nil];
  189. // // NSData *data = [NSData dataWithContentsOfFile:path];
  190. // // vc.content_data_download = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil] mutableCopy];
  191. // [self.navigationController pushViewController:vc animated:NO];
  192. // return;
  193. // }
  194. // }
  195. // else
  196. // {
  197. //
  198. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];
  199. // //block代码块取代了delegate
  200. //
  201. // __weak typeof(self) weakSelf = self;
  202. // for(int i=0;i<self.content_action.count;i++)
  203. // {
  204. // NSMutableDictionary* json = self.content_action[i];
  205. // NSString* title =json[@"title"];
  206. // NSString* module = json[@"module"];
  207. // NSMutableDictionary* add_params = json[@"params"];
  208. // UIAlertAction *actioni = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  209. //
  210. //
  211. // if([module isEqualToString:@"quick_look"])
  212. // {
  213. //
  214. // // +(NSString*)download_file : (NSMutableDictionary*) params url:(NSString*) url
  215. // NSMutableDictionary *params = json[@"params"];
  216. // NSString* URL =json[@"url"];
  217. // __weak typeof(self) weakSelf = self;
  218. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  219. //
  220. //
  221. // NSString *path = [RANetwork download_file:params url:URL];
  222. // weakSelf.documentPath = path;
  223. //
  224. // if (path) {
  225. // dispatch_async(dispatch_get_main_queue(), ^{
  226. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  227. // });
  228. // }
  229. //
  230. // });
  231. // }
  232. // else if([module isEqualToString:@"kv_detail"])
  233. // {
  234. // NSMutableDictionary *params = json[@"params"];
  235. //
  236. //
  237. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Loading..." title:@"Please wait"];
  238. //
  239. //
  240. // __weak typeof(self) weakself = self;
  241. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  242. //
  243. //
  244. //
  245. // NSDictionary* json =[RANetwork kv_detail:params];
  246. //
  247. // dispatch_async(dispatch_get_main_queue(), ^{
  248. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  249. //
  250. //
  251. // if([[json valueForKey:@"result"] intValue]==2)
  252. // {
  253. // KVViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"KVViewController"];
  254. // vc.content = [json mutableCopy];
  255. // [weakself.navigationController pushViewController:vc animated:NO];
  256. //
  257. // // weakself.content_data_download = [weakself CopyDirty:weakself.content_data_download to:[editor_json mutableCopy]];//[editor_json mutableCopy];
  258. // //
  259. // // weakself.content_data_control = [weakself translate_json:weakself.content_data_download changed: weakself.changed_data];
  260. // //
  261. // //
  262. // // [weakself download_success];
  263. // // [weakself.editorTable reloadData];
  264. //
  265. // }
  266. // else
  267. // {
  268. // [RAUtils message_alert:json[@"msg"] title:nil controller:weakself] ;
  269. // }
  270. //
  271. //
  272. //
  273. //
  274. // });
  275. // });
  276. // }
  277. // else if([module isEqualToString:@"order_detail"])
  278. // {
  279. //
  280. // OrderDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
  281. //
  282. //
  283. // vc.url_type = URL_REMOTE;
  284. // vc.request_url=URL_ORDER_DETAIL;
  285. //
  286. //
  287. // vc.params = [NSMutableDictionary dictionary];
  288. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"search_ui.json" ofType:nil];
  289. // // NSData *data = [NSData dataWithContentsOfFile:path];
  290. // // vc.content_data_download = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil] mutableCopy];
  291. // [self.navigationController pushViewController:vc animated:NO];
  292. // return;
  293. // }
  294. // // DebugLog(@"Cancel");
  295. //// if([actiontype isEqualToString:@"download"])
  296. //// {
  297. //// [weakSelf Export:add_params];
  298. //// }
  299. //// else if([actiontype isEqualToString:@"save"])
  300. //// {
  301. //// }
  302. // }];
  303. // [alertControl addAction:actioni];
  304. // }
  305. //
  306. // UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  307. // // DebugLog(@"Cancel");
  308. //
  309. // }];
  310. // [alertControl addAction:actionCancel];
  311. //
  312. //
  313. //
  314. //
  315. //
  316. // [self presentViewController:alertControl animated:NO completion:nil];
  317. //
  318. // }
  319. //
  320. // }
  321. //
  322. //
  323. //
  324. //}
  325. -(void)labelTapAction:(UIGestureRecognizer*)gestureRecognizer
  326. {
  327. NSLog(@"label touched");
  328. MDHTMLLabel* label = (MDHTMLLabel*)[gestureRecognizer view];
  329. self.label.htmlText = label.htmlText;
  330. [self.label sizeToFit];
  331. if(self.label.frame.size.width>label.frame.size.width||self.label.frame.size.height>label.frame.size.height)
  332. {
  333. FullyShowViewController *fullVC = [ self.storyboard instantiateViewControllerWithIdentifier:@"fullyShowViewController"];
  334. fullVC.htmlText = label.htmlText;
  335. fullVC.preferredContentSize = CGSizeMake(200, 240);
  336. fullVC.modalPresentationStyle=UIModalPresentationPopover;
  337. UIPopoverPresentationController* popPc = fullVC.popoverPresentationController;
  338. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  339. popPc.sourceView = label;
  340. popPc.delegate = self;
  341. [self presentViewController:fullVC animated:true completion:nil];
  342. }
  343. }
  344. - (void)didReceiveMemoryWarning {
  345. [super didReceiveMemoryWarning];
  346. // Dispose of any resources that can be recreated.
  347. }
  348. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  349. }
  350. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  351. {
  352. // auto resizing
  353. // self.scrollview.contentSize=self.tableview.frame.size;
  354. // auto layout
  355. // int width=[self.content_layout[@"header"][@"width"] intValue];
  356. // if(width<self.view.frame.size.width)
  357. // width=self.view.frame.size.width;
  358. // self.w_constraint.constant = width - CGRectGetWidth(self.view.bounds);
  359. // self.h_constraint.constant = 0;
  360. [self updateTableFrame];
  361. }
  362. #pragma mark - TableView DataSource
  363. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  364. return [self.content_data[@"count"] intValue];
  365. }
  366. //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  367. //{
  368. // return 1;
  369. //}
  370. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  371. {
  372. int height=[self.content_layout[@"header"][@"height"] intValue];
  373. if(height==0)
  374. height=44;
  375. return height;
  376. }
  377. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  378. {
  379. NSArray* arr_col=self.content_layout[@"header"][@"col"];
  380. NSString* value =self.content_layout[@"header"][@"f_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  381. if(value==nil)
  382. value=@"";
  383. unsigned long fcolor = strtoul([value UTF8String],0,16);
  384. value =self.content_layout[@"header"][@"bg_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  385. if(value==nil)
  386. value=@"";
  387. unsigned long bgcolor = strtoul([value UTF8String],0,16);
  388. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  389. myView.backgroundColor = UIColorFromRGB(bgcolor);
  390. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  391. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  392. myView.layer.shadowOffset = CGSizeMake(0, 0);
  393. myView.layer.shadowOpacity = 0.5;
  394. myView.layer.shadowRadius = 2.0;
  395. int x=0;//[self.content_layout[@"header"][@"margin_l"] intValue];
  396. int y=0;//[self.content_layout[@"header"][@"margin_t"] intValue];;
  397. int height = [self.content_layout[@"header"][@"height"] intValue];
  398. for(int i=0;i<arr_col.count;i++)
  399. {
  400. int width = [arr_col[i][@"width"] intValue];
  401. UILabel *headerlabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
  402. headerlabel.textColor=UIColorFromRGB(fcolor);//UIColor.blackColor;
  403. headerlabel.backgroundColor = [UIColor clearColor];
  404. headerlabel.text=arr_col[i][@"name"];
  405. headerlabel.textAlignment= [self get_TextHAlign:arr_col[i][@"h_align"] ];
  406. headerlabel.font = [UIFont boldSystemFontOfSize:20];
  407. // [headerlabel sizeToFit];
  408. headerlabel.layer.borderWidth=0.5;
  409. [myView addSubview:headerlabel];
  410. x+=width;
  411. }
  412. myView.autoresizesSubviews=true;
  413. // [myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
  414. // modellabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  415. // pricelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  416. // timelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  417. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  418. //
  419. return myView;
  420. }
  421. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  422. {
  423. int height=[self.content_layout[@"row"][@"height"] intValue];
  424. if(height==0)
  425. height=44;
  426. return height;
  427. }
  428. -(NSTextAlignment) get_TextHAlign:(NSString*)textHAlign
  429. {
  430. if([textHAlign.lowercaseString isEqualToString:@"center"])
  431. return NSTextAlignmentCenter;
  432. else if([textHAlign.lowercaseString isEqualToString:@"left"])
  433. return NSTextAlignmentLeft;
  434. else if([textHAlign.lowercaseString isEqualToString:@"right"])
  435. return NSTextAlignmentRight;
  436. return NSTextAlignmentLeft;
  437. }
  438. //-(RTTextAlignment) get_RTTextHAlign:(NSString*)textHAlign
  439. //{
  440. //
  441. //
  442. //
  443. //
  444. // if([textHAlign.lowercaseString isEqualToString:@"center"])
  445. // return RTTextAlignmentCenter;
  446. // else if([textHAlign.lowercaseString isEqualToString:@"left"])
  447. // return RTTextAlignmentLeft;
  448. // else if([textHAlign.lowercaseString isEqualToString:@"right"])
  449. // return RTTextAlignmentRight;
  450. //
  451. // return RTTextAlignmentLeft;
  452. //}
  453. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  454. {
  455. {
  456. NSString *CellIdentifier = @"tCell";
  457. UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  458. for(UIGestureRecognizer* g in cell.gestureRecognizers)
  459. [ cell removeGestureRecognizer:g];
  460. // cell gesture
  461. // [cell removeGestureRecognizer:self.cellDoubleTap];
  462. for(UIView* v in cell.subviews)
  463. {
  464. for(UIGestureRecognizer* g in v.gestureRecognizers)
  465. [ v removeGestureRecognizer:g];
  466. [v removeFromSuperview];
  467. }
  468. // cell.detailTextLabel.text=@"detail";
  469. // cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
  470. UITapGestureRecognizer *cellDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellDoubleTapAction:)];
  471. cellDoubleTap.numberOfTouchesRequired = 1; //手指数
  472. cellDoubleTap.numberOfTapsRequired = 2; //tap次数
  473. [cell addGestureRecognizer:cellDoubleTap];
  474. NSArray* arr_col=self.content_layout[@"header"][@"col"];
  475. NSArray* item =self.content_data[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  476. NSString* value =self.content_layout[@"row"][@"f_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  477. if(value==nil)
  478. value=@"";
  479. unsigned long fcolor = strtoul([value UTF8String],0,16);
  480. value =self.content_layout[@"row"][@"color_0"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  481. if(value==nil)
  482. value=@"";
  483. unsigned long color0 = strtoul([value UTF8String],0,16);
  484. value =self.content_layout[@"row"][@"color_1"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  485. if(value==nil)
  486. value=@"";
  487. unsigned long color1 = strtoul([value UTF8String],0,16);
  488. int x=0;//[self.content_layout[@"header"][@"margin_l"] intValue];
  489. int y=0;//[self.content_layout[@"header"][@"margin_t"] intValue];;
  490. int height = [self.content_layout[@"row"][@"height"] intValue];
  491. for(int i=0;i<arr_col.count;i++)
  492. {
  493. int width = [arr_col[i][@"width"] intValue];;
  494. MDHTMLLabel *vallabel = [[MDHTMLLabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
  495. vallabel.textColor=UIColorFromRGB(fcolor);//UIColor.blackColor;
  496. vallabel.backgroundColor = [UIColor clearColor];
  497. NSString* text=@"";
  498. if(i>item.count-1)
  499. text=@"row data missing";
  500. else
  501. text=item[i];
  502. // RTTextLineBreakModeWordWrapping = kCTLineBreakByWordWrapping,
  503. // RTTextLineBreakModeCharWrapping = kCTLineBreakByCharWrapping,
  504. // RTTextLineBreakModeClip = kCTLineBreakByClipping,
  505. // NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
  506. // vallabel.attributedText = attrStr;
  507. vallabel.htmlText = text;
  508. vallabel.textAlignment= [self get_TextHAlign:arr_col[i][@"h_align"] ];
  509. vallabel.layer.borderWidth=0.5;
  510. // UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapAction:)];
  511. UITapGestureRecognizer* labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)];
  512. labelTap.numberOfTouchesRequired = 1; //手指数
  513. labelTap.numberOfTapsRequired = 1; //tap次数
  514. [vallabel addGestureRecognizer:labelTap];
  515. [labelTap requireGestureRecognizerToFail:cellDoubleTap];
  516. // [vallabel addGestureRecognizer:self.cellDoubleTap];
  517. vallabel.userInteractionEnabled = YES;
  518. // [vallabel becomeFirstResponder];
  519. // vallabel.delegate = self;
  520. // [statuslabel sizeToFit];
  521. [cell addSubview:vallabel];
  522. x+=width;
  523. }
  524. if(indexPath.row%2==0)
  525. cell.backgroundColor=UIColorFromRGB(color0);//[UIColor whiteColor];
  526. else
  527. cell.backgroundColor=UIColorFromRGB(color1);//[UIColor lightGrayColor];
  528. return cell;
  529. }
  530. }
  531. //#pragma mark - TouchLabel Delegate
  532. //- (void)touchLabel:(TouchLabel *)touchLabel touchesWtihTag:(NSInteger)tag
  533. //{
  534. //
  535. //}
  536. //-(void) empty
  537. //{
  538. //
  539. //}
  540. #pragma mark - UIPopoverPresentationControllerDelegate Delegate
  541. //实现该代理方法,返回UIModalPresentationNone值,可以在iPhone设备实现popover效果
  542. -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  543. return UIModalPresentationNone;//不适配(不区分ipad或iPhone)
  544. }
  545. #pragma mark - TableView Delegate
  546. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  547. {
  548. NSLog(@"cell selected");
  549. }
  550. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  551. return UITableViewCellEditingStyleDelete;
  552. }
  553. #pragma mark - Public
  554. - (void)setQueryParams:(NSDictionary *)params {
  555. self.params = params;
  556. }
  557. #pragma mark - Private
  558. - (void)setupTableRefreshView {
  559. self.tableview.jl_header = [[JLRefreshHeader alloc] init];
  560. self.tableview.jl_header.refreshDelegate = self;
  561. self.tableview.jl_footer = [[JLRefreshFooter alloc] init];
  562. self.tableview.jl_footer.refreshDelegate = self;
  563. }
  564. - (void)setupNavigationBar {
  565. if(self.content_menu==nil)
  566. {
  567. self.navigationItem.rightBarButtonItem=nil;
  568. return;
  569. }
  570. UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStylePlain target:self action:@selector(MenuClick:)];
  571. self.navigationItem.rightBarButtonItem = menuItem;
  572. }
  573. - (void)MenuClick:(UIBarButtonItem *)sender {
  574. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Menu" message:nil preferredStyle:UIAlertControllerStyleAlert];
  575. //block代码块取代了delegate
  576. __weak typeof(self) weakSelf = self;
  577. for(int i=0;i<self.content_menu.count;i++)
  578. {
  579. NSMutableDictionary* json = self.content_menu[i];
  580. NSString* title =json[@"title"];
  581. NSString* actiontype = json[@"action"];
  582. NSString* url = json[@"url"];
  583. // NSMutableDictionary* add_params = json[@"params"];
  584. UIAlertAction *actioni = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  585. // DebugLog(@"Cancel");
  586. if([actiontype isEqualToString:@"download"])
  587. {
  588. [weakSelf Export:url];
  589. }
  590. else if([actiontype isEqualToString:@"save"])
  591. {
  592. }
  593. }];
  594. [alertControl addAction:actioni];
  595. }
  596. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  597. // DebugLog(@"Cancel");
  598. }];
  599. [alertControl addAction:actionCancel];
  600. [self presentViewController:alertControl animated:NO completion:nil];
  601. }
  602. -(void)Export:(NSString*) url
  603. {
  604. if(self.download_query )
  605. {
  606. self.documentPath = self.download_query;
  607. [self.navigationController pushViewController:self.quickLook animated:NO];
  608. return;
  609. }
  610. __weak typeof(self) weakSelf = self;
  611. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  612. // NSMutableDictionary *params = self.params.mutableCopy;
  613. // if(add_params!=nil)
  614. // [params addEntriesFromDictionary:add_params];
  615. NSString *path = [RANetwork download_query:url];
  616. weakSelf.download_query = path;
  617. weakSelf.documentPath = path;
  618. if (path) {
  619. dispatch_async(dispatch_get_main_queue(), ^{
  620. [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  621. });
  622. }
  623. });
  624. }
  625. - (void)downloadItemClick:(UIBarButtonItem *)sender {
  626. if(self.download_query )
  627. {
  628. self.documentPath = self.download_query;
  629. [self.navigationController pushViewController:self.quickLook animated:NO];
  630. return;
  631. }
  632. __weak typeof(self) weakSelf = self;
  633. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  634. NSMutableDictionary *params = self.params.mutableCopy;
  635. NSString *path = [RANetwork download_query:params];
  636. weakSelf.download_query = path;
  637. weakSelf.documentPath = path;
  638. if (path) {
  639. dispatch_async(dispatch_get_main_queue(), ^{
  640. [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  641. });
  642. }
  643. });
  644. }
  645. - (NSInteger)resultItemCount {
  646. return [[self.content_data objectForKey:@"count"] integerValue];
  647. }
  648. - (void)loadContent {
  649. [self loadContentWithOption:0 Complete:nil];
  650. }
  651. /**
  652. * 0: init load
  653. * 1: refresh
  654. * 2: load more
  655. */
  656. - (void)loadContentWithOption:(int)option Complete:(void (^)(int result,int count))finish {
  657. if (self.params) {
  658. NSMutableDictionary *dic = self.params.mutableCopy;
  659. switch (option) {
  660. case 0:
  661. case 1: {
  662. self.offset = 0;
  663. self.content_layout =nil;
  664. self.content_action = nil;
  665. self.content_menu = nil;
  666. self.content_data = nil;
  667. }
  668. break;
  669. case 2: {
  670. self.offset = [self resultItemCount];
  671. }
  672. break;
  673. }
  674. // dic[@"criteria"] = self.params;
  675. [dic setObject:@(self.offset) forKey:@"offset"];
  676. [dic setObject:@(delta) forKey:@"limit"];
  677. // dic[@"criteria"] = [NSArray arrayWithObjects:@"aaa",@"bbb", nil];
  678. __weak typeof(self) weakSelf = self;
  679. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  680. NSDictionary *contentDic = [RANetwork query:dic];
  681. NSInteger result = [[contentDic objectForKey:@"result"] integerValue];
  682. dispatch_async(dispatch_get_main_queue(), ^{
  683. if (weakSelf) {
  684. __strong typeof(weakSelf) strongSelf = weakSelf;
  685. if (result == RESULT_TRUE) {
  686. strongSelf.content_layout = [contentDic objectForKey:@"layout"];
  687. strongSelf.content_action = [contentDic objectForKey:@"row_action"];
  688. strongSelf.content_menu = [[contentDic objectForKey:@"menu"] mutableCopy];
  689. [strongSelf setupNavigationBar];
  690. UILabel * headerview=(UILabel *)self.tableview.tableHeaderView;
  691. headerview.text = contentDic[@"table_title"];
  692. [self resize_tableHeader];
  693. if(strongSelf.content_data==nil)
  694. strongSelf.content_data = [NSMutableDictionary new];
  695. NSMutableDictionary* newdata = [[contentDic objectForKey:@"data"] mutableCopy];
  696. for(int dc = 0;dc<[newdata[@"count"] intValue];dc++)
  697. {
  698. NSMutableDictionary * newdata_item = newdata[[NSString stringWithFormat:@"item_%d",dc]];
  699. strongSelf.content_data[[NSString stringWithFormat:@"item_%d",[strongSelf.content_data[@"count"] intValue] ]] = newdata_item;
  700. strongSelf.content_data[@"count"] = @([strongSelf.content_data[@"count"] intValue]+1);
  701. }
  702. // strongSelf.content_data = [contentDic objectForKey:@"data"];
  703. [strongSelf updateTableFrame];
  704. [strongSelf.tableview reloadData];
  705. } else {
  706. [RAUtils message_alert:[contentDic objectForKey:@"err_msg"] title:@"Warning" controller:strongSelf];
  707. }
  708. if (finish) {
  709. finish((int)result,(int)[self resultItemCount]);
  710. }
  711. }
  712. });
  713. });
  714. }
  715. }
  716. - (void)updateTableFrame {
  717. // auto resizing
  718. // int width=[self.content_layout[@"header"][@"width"] intValue];
  719. // if(width<self.tableview.frame.size.width)
  720. // width=self.tableview.frame.size.width;
  721. //
  722. // CGRect frame = CGRectMake(self.tableview.frame.origin.x, self.tableview.frame.origin.y, width, self.tableview.frame.size.height);
  723. // self.tableview.frame=frame;
  724. // self.scrollview.contentSize=self.tableview.frame.size;
  725. // auto layout
  726. int width=[self.content_layout[@"header"][@"width"] intValue];
  727. if(width<self.view.frame.size.width)
  728. width=self.view.frame.size.width;
  729. self.w_constraint.constant = width - CGRectGetWidth(self.view.bounds);
  730. self.h_constraint.constant = 0;
  731. // 约束修改后立即重新布局
  732. [self.view layoutIfNeeded];
  733. if (@available(iOS 11.0,*)) {
  734. UIEdgeInsets edgeInsets = [self.view safeAreaInsets];
  735. CGFloat left = edgeInsets.left;
  736. CGFloat right = edgeInsets.right;
  737. self.lead_constraint.constant = left;
  738. self.trail_constraint.constant = right;
  739. /*
  740. * lead 和 trail改变之前
  741. * scrollview.size.width = self.view.size.width;
  742. * 改变后
  743. * scrollview.size.width = self.view.size.width - left - right;
  744. * 由于w_contraint本质上是根据scrolliew.size.width决定,实际上使用self.view.size.width计算得到
  745. * 所以w_contraint需要 +left +right补偿偏差。
  746. */
  747. self.w_constraint.constant += left + right;
  748. // 约束修改后立即重新布局
  749. [self.view layoutIfNeeded];
  750. }
  751. }
  752. #pragma mark - RefreshDelegate
  753. - (void)jl_pullRefresh:(JLRefreshBasis *)refresh state:(JLRefreshState)state percentage:(float)percentage {
  754. if ([refresh isEqual:self.tableview.jl_header]) {
  755. }
  756. if (refresh == self.tableview.jl_footer) {
  757. }
  758. }
  759. - (void)jl_endRefresh:(JLRefreshBasis *)refresh {
  760. // state == idle
  761. // refresh UI
  762. [self.tableview reloadData];
  763. if (refresh == self.tableview.jl_footer && refresh.state == JLRefreshStateNoMore) {
  764. }
  765. }
  766. - (void)jl_beginRefresh:(JLRefreshBasis *)refresh {
  767. // state == refreshing
  768. // load data
  769. // finish loading data
  770. int option = 0;
  771. if ([refresh isEqual:self.tableview.jl_header]) {
  772. option = 1;
  773. }
  774. if (refresh == self.tableview.jl_footer) {
  775. if(refresh.state==JLRefreshStateNoMore) //数据取完不再加载
  776. return;
  777. option = 2;
  778. }
  779. __weak typeof(self) weakSelf = self;
  780. [self loadContentWithOption:option Complete:^(int result, int count) {
  781. if ([refresh isEqual:weakSelf.tableview.jl_header]) {
  782. [weakSelf.tableview.jl_header performSelector:@selector(endRefresh)];
  783. }
  784. if (refresh == weakSelf.tableview.jl_footer) {
  785. [weakSelf.tableview.jl_footer performSelector:@selector(endRefresh)];
  786. if (result == 2 && count < delta) {
  787. [weakSelf.tableview.jl_footer performSelector:@selector(noMoreData)];
  788. }
  789. }
  790. }];
  791. }
  792. #pragma mark - Quick Look
  793. - (QLPreviewController *)quickLook {
  794. if (!_quickLook)
  795. {
  796. _quickLook = [[QLPreviewController alloc] init];
  797. _quickLook.delegate = self;
  798. _quickLook.dataSource = self;
  799. }
  800. else
  801. {
  802. [_quickLook reloadData];
  803. }
  804. return _quickLook;
  805. }
  806. #pragma mark - QuickLook Data Source
  807. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
  808. return 1;
  809. }
  810. - (id <QLPreviewItem>) previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  811. return [NSURL fileURLWithPath:self.documentPath];
  812. }
  813. #pragma mark - QuickLook Delegate
  814. - (void) previewControllerDidDismiss:(QLPreviewController *)controller {
  815. }
  816. /**
  817. * 文件内部链接点击不进行外部跳转
  818. */
  819. - (BOOL) previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
  820. return NO;
  821. }
  822. #pragma mark - customize editor
  823. - (UIView*)get_tableHeader
  824. {
  825. UILabel * headerview = [UILabel new];
  826. headerview.frame = CGRectMake(0, 0, self.tableview.frame.size.width, 44);
  827. headerview.numberOfLines = 0;
  828. headerview.textAlignment = NSTextAlignmentCenter;
  829. headerview.text = @"";
  830. return headerview;
  831. }
  832. -(void) resize_tableHeader
  833. {
  834. //利用systemLayoutSizeFittingSize:计算出真实高度
  835. CGFloat height = [self.tableview.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  836. CGRect headerFrame = self.tableview.tableHeaderView.frame;
  837. headerFrame.size.height = height+20;//20是留的上下边距
  838. //修改tableHeaderView的frame
  839. self.tableview.tableHeaderView.frame = headerFrame;
  840. }
  841. @end