|
|
@@ -0,0 +1,283 @@
|
|
|
+//
|
|
|
+// KVViewController.m
|
|
|
+// RedAnt Mobile
|
|
|
+//
|
|
|
+// Created by Ray on 15/11/2017.
|
|
|
+// Copyright © 2017 Ray. All rights reserved.
|
|
|
+//
|
|
|
+#define DEF_CELL_HEIGHT 44
|
|
|
+#define DEF_TABLE_HEIGHT 44
|
|
|
+#define LINE_WIDTH 0
|
|
|
+#define CELL_MARGIN 0
|
|
|
+#define LABEL_MARGIN 5
|
|
|
+#import "KVViewController.h"
|
|
|
+#import "RTLabel.h"
|
|
|
+#import "DetailKVCell.h"
|
|
|
+#import "LineView.h"
|
|
|
+
|
|
|
+@interface KVViewController ()
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation KVViewController
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+}
|
|
|
+
|
|
|
+- (void)didReceiveMemoryWarning {
|
|
|
+ [super didReceiveMemoryWarning];
|
|
|
+ // Dispose of any resources that can be recreated.
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+#pragma mark - Navigation
|
|
|
+
|
|
|
+// In a storyboard-based application, you will often want to do a little preparation before navigation
|
|
|
+- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
+ // Get the new view controller using [segue destinationViewController].
|
|
|
+ // Pass the selected object to the new view controller.
|
|
|
+}
|
|
|
+*/
|
|
|
+
|
|
|
+#pragma mark - Table view data source
|
|
|
+
|
|
|
+//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
|
|
|
+//{
|
|
|
+// return 40;
|
|
|
+//}
|
|
|
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
|
|
|
+{
|
|
|
+// DebugLog(@"heightForRowAtIndexPath");
|
|
|
+
|
|
|
+ // float height = [self.content itemHeightAtIndexPath:indexPath];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ float width = tableView.frame.size.width;
|
|
|
+ width-=CELL_MARGIN*2;
|
|
|
+ CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
|
|
|
+ CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
|
|
|
+
|
|
|
+
|
|
|
+ NSDictionary* section = self.content[[NSString stringWithFormat:@"section_%ld",indexPath.section] ];
|
|
|
+
|
|
|
+ NSDictionary* item = section[@"data"][indexPath.row];
|
|
|
+ NSString* key =item[@"key"];
|
|
|
+ // [cell.keyLabel sizeToFit];
|
|
|
+ NSString* val=item[@"val"];
|
|
|
+ if([val isEqual:[NSNull null]])
|
|
|
+ val=@"";
|
|
|
+ if(val==nil)
|
|
|
+ val=@"";
|
|
|
+ if([val isEqualToString:@"null"])
|
|
|
+ val=@"";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ CGRect frame;
|
|
|
+ frame.size = constraintval;
|
|
|
+ frame.origin.x=0;
|
|
|
+ frame.origin.y=0;
|
|
|
+ RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
|
|
|
+ [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
|
|
|
+ // rtlabel.lineSpacing = 20.0;
|
|
|
+ [rtlabel setText: val];
|
|
|
+ CGSize sizeval=rtlabel.optimumSize;
|
|
|
+
|
|
|
+
|
|
|
+ CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
|
|
|
+ // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
|
|
|
+ float height = MAX(sizekey.height,sizeval.height);
|
|
|
+ height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
|
|
|
+
|
|
|
+ /*
|
|
|
+ NSString *text = [items objectAtIndex:[indexPath row]];
|
|
|
+
|
|
|
+ CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
|
|
|
+
|
|
|
+ CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
|
|
|
+
|
|
|
+ CGFloat height = MAX(size.height, 44.0f);
|
|
|
+
|
|
|
+ return height + (CELL_CONTENT_MARGIN * 2);
|
|
|
+ */
|
|
|
+ // NSString *CellIdentifier = @"detail_item_kv";
|
|
|
+ // DetailCellKV *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
|
|
|
+ // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
|
|
|
+ // cell.keyLabel.text=[item allKeys][0];
|
|
|
+ // [cell.keyLabel sizeToFit];
|
|
|
+ // cell.valLabel.text= [item valueForKey:cell.keyLabel.text];
|
|
|
+ // [cell.valLabel sizeToFit];
|
|
|
+ //
|
|
|
+ // return MAX(cell.keyLabel.frame.size.height,cell.valLabel.frame.size.height);
|
|
|
+ return height;
|
|
|
+
|
|
|
+
|
|
|
+ // return height;
|
|
|
+}
|
|
|
+
|
|
|
+- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
|
|
|
+{
|
|
|
+ UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
|
|
|
+ myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
|
|
|
+ UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 90, 22)];
|
|
|
+ titleLabel.textColor=[UIColor whiteColor];
|
|
|
+ titleLabel.backgroundColor = [UIColor clearColor];
|
|
|
+
|
|
|
+ NSDictionary* json = self.content[[NSString stringWithFormat:@"section_%ld",section] ];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ titleLabel.text =json[@"title"];
|
|
|
+ [titleLabel sizeToFit];
|
|
|
+ [myView addSubview:titleLabel];
|
|
|
+
|
|
|
+ return myView;
|
|
|
+}
|
|
|
+
|
|
|
+//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
|
|
|
+//{
|
|
|
+// UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
|
|
|
+// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
|
|
|
+//// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
|
|
|
+//// titleLabel.textColor=[UIColor whiteColor];
|
|
|
+//// titleLabel.backgroundColor = [UIColor grayColor];
|
|
|
+//// titleLabel.text =[self.content get_segmentname:section];
|
|
|
+//// [titleLabel sizeToFit];
|
|
|
+//// [myView addSubview:titleLabel];
|
|
|
+//
|
|
|
+// return myView;
|
|
|
+//}
|
|
|
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ return [self.content[@"count"] intValue];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
+{
|
|
|
+
|
|
|
+ NSDictionary* json = self.content[[NSString stringWithFormat:@"section_%ld",section] ];
|
|
|
+ return [json[@"data"] count];
|
|
|
+
|
|
|
+}
|
|
|
+- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
|
+
|
|
|
+ NSDictionary* json = self.content[[NSString stringWithFormat:@"section_%ld",section] ];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return json[@"title"];
|
|
|
+}
|
|
|
+//- (UIImage *) imageWithTintColor:(UIImage*)source Color:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode {
|
|
|
+// //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
|
|
|
+// UIGraphicsBeginImageContextWithOptions(source.size, NO, 0.0f); [tintColor setFill];
|
|
|
+// CGRect bounds = CGRectMake(0, 0, source.size.width, source.size.height); UIRectFill(bounds);
|
|
|
+// //Draw the tinted image in context
|
|
|
+// [source drawInRect:bounds blendMode:blendMode alpha:1.0f];
|
|
|
+// if (blendMode != kCGBlendModeDestinationIn)
|
|
|
+// {
|
|
|
+// [source drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
|
|
|
+// }
|
|
|
+// UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
|
+// UIGraphicsEndImageContext();
|
|
|
+// return tintedImage;
|
|
|
+//}
|
|
|
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // NSString *CellIdentifier = @"testcell";
|
|
|
+ // return [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ float width = tableView.frame.size.width;
|
|
|
+
|
|
|
+ width-=CELL_MARGIN*2;
|
|
|
+ CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
|
|
|
+ CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
|
|
|
+ NSDictionary* section = self.content[[NSString stringWithFormat:@"section_%ld",indexPath.section] ];
|
|
|
+
|
|
|
+ NSDictionary* item = section[@"data"][indexPath.row];
|
|
|
+ NSString* key =item[@"key"];
|
|
|
+ // [cell.keyLabel sizeToFit];
|
|
|
+ NSString* val=item[@"val"];
|
|
|
+ if([val isEqual:[NSNull null]])
|
|
|
+ val=@"";
|
|
|
+ if(val==nil)
|
|
|
+ val=@"";
|
|
|
+ if([val isEqualToString:@"null"])
|
|
|
+ val=@"";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ CGRect frame;
|
|
|
+ frame.size = constraintval;
|
|
|
+ frame.origin.x=0;
|
|
|
+ frame.origin.y=0;
|
|
|
+ RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
|
|
|
+ [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
|
|
|
+ // rtlabel.lineSpacing = 20.0;
|
|
|
+ [rtlabel setText: val];
|
|
|
+ CGSize sizeval=rtlabel.optimumSize;
|
|
|
+ CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
|
|
|
+
|
|
|
+ float height = MAX(sizekey.height,sizeval.height);
|
|
|
+ height = MAX(height, DEF_CELL_HEIGHT-LINE_WIDTH);
|
|
|
+ sizekey.height = height;
|
|
|
+ sizekey.width = constraintkey.width;
|
|
|
+ // sizeval.height = height;
|
|
|
+ sizeval.width =constraintval.width;
|
|
|
+
|
|
|
+ NSString *CellIdentifier = @"detail_item_kv";
|
|
|
+ DetailKVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ CGRect framekey = cell.keyLabel.frame;
|
|
|
+ framekey.origin.x=CELL_MARGIN+LABEL_MARGIN;
|
|
|
+ framekey.origin.y=LINE_WIDTH;
|
|
|
+ framekey.size = sizekey;
|
|
|
+ cell.keyLabel.frame = framekey;
|
|
|
+ cell.keyLabel.text=key;
|
|
|
+ CGRect frameval = cell.valLabel.frame;
|
|
|
+ frameval.origin.x=constraintkey.width+CELL_MARGIN+3*LABEL_MARGIN; //2 margin for key 1 margin for value self
|
|
|
+ frameval.origin.y=LINE_WIDTH;
|
|
|
+ frameval.size = sizeval;
|
|
|
+ cell.valLabel.frame = frameval;
|
|
|
+ // [cell.valLabel setTextAlignment:RTTextAlignmentCenter];
|
|
|
+ cell.valLabel.text= val;
|
|
|
+ CGPoint rcenter;
|
|
|
+ rcenter.x = width * 0.4+width * 0.6/2;
|
|
|
+ rcenter.y = height /2;
|
|
|
+ cell.valLabel.center=rcenter;
|
|
|
+ // cell.valLabel.backgroundColor =[UIColor redColor];
|
|
|
+
|
|
|
+ // cell.contentView.superview.backgroundColor = [UIColor clearColor];
|
|
|
+ // cell.backgroundColor = [UIColor whiteColor];
|
|
|
+ UIView * lineview = [[LineView alloc] initWithFrame:cell.contentView.frame];
|
|
|
+ lineview.userInteractionEnabled = NO;// 不设为NO会屏蔽cell的点击事件
|
|
|
+ lineview.backgroundColor = [UIColor clearColor];// 设为透明从而使得cell.backgroundColor有效.
|
|
|
+ lineview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
+ [cell.contentView addSubview:lineview];// cell.contentView是个readonly属性,所以别想着替换contentView了.
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return cell;
|
|
|
+
|
|
|
+
|
|
|
+ return nil;
|
|
|
+}
|
|
|
+@end
|