ResultViewController.m 41 KB

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