ResultViewController.m 39 KB

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