ResultViewController.m 42 KB

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