//
//  DetailPageViewController.m
//  Apex Mobile
//
//  Created by Ray on 14-3-8.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "DetailPageViewController.h"
#define  DEF_CELL_HEIGHT 44
#define  DEF_TABLE_HEIGHT 44
#define  LINE_WIDTH 0
#define  CELL_MARGIN 0
#define  LABEL_MARGIN 5
#import "TabBarController.h"
@interface DetailPageViewController ()

@end

@implementation DetailPageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)changeCell
{
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
    [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
      DebugLog(@"refresh!!!!!!!!");
    
}

-(void)ReloadData
{
    
    //    Count ++ ;
    //    [mytabelview reloadData];
    [self loadpage];
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    
    [reF endRefreshing];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    self.webviewoprationQueue = [[NSOperationQueue alloc] init];
//    self.webviewoprationQueue.maxConcurrentOperationCount = 1;
    
    UIRefreshControl *ref = [[UIRefreshControl alloc]init];
    ref.tag = 200 ;
    ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
    ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
    //    ref.hidden = true;
    [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
    
    [self.table addSubview:ref];
    if(self.tabBarController.tabBar.hidden==true)
    {
        CGRect frame = self.table.frame;
        frame.size.height +=40;
        self.table.frame=frame;
    }
    //        self.view.backgroundColor= [UIColor whiteColor];
    [self loadpage];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) loadpage
{
    
    //    [self.params setValue:[NSString stringWithFormat:@"%d",self.offset] forKey:@"offset"];
    //    self.prepageButton.enabled=false;
    //    self.nextpageButton.enabled=false;
    self.mum.center = self.view.center;
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    self.mum.hidden = false;
    self.table.hidden = true;
    
    [UIView commitAnimations];
    
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        self.content=[ApexMobileNetwork get_detailcontent:self.params];
        
        
        //        PageData* pageData=nil;
        //        if(self.recordCount==-1)
        //            self.recordCount=[ApexMobileNetwork get_recordcount: self.params ];
        //        if(self.recordCount>0)
        //        {
        //            self.pageData=[ApexMobileNetwork get_pagedata:self.params];
        //            //            self.pageData=[ApexMobileNetwork]
        //        }
        //
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            NSTimeInterval animationDuration = 0.30f;
            [UIView beginAnimations:@"ResizeView" context:nil];
            [UIView setAnimationDuration:animationDuration];
            
            
            self.mum.hidden = true;
            if(self.content.result_code!=RESULT_TRUE)
            {
                self.table.hidden =false;
                [self.table reloadData];
                
            }
            else
            if([self.content get_segmentcount]==0)
            {
                self.table.hidden =true;
                self.norecordLabel.hidden=false;
            }
            else
            {
                self.table.hidden =false;
                [self.table reloadData];
            }
            [UIView commitAnimations];
            
            NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
            bool autologin =[defaults boolForKey:@"autologin"];
            if(!autologin)
            {
                TabBarController * viewController =[self.storyboard instantiateViewControllerWithIdentifier:@"LoginTabBar"];
                [self presentViewController:viewController animated:YES completion:^{
                    
                }];
            }
            
            //            [self.table setNeedsDisplay];
            //            if(self.recordCount<2000)
            //                self.countView.title = [NSString stringWithFormat:@"%d records",self.recordCount];
            //            else
            //                self.countView.title = @"2000+ records";
            //
            //            if(self.offset<self.pageData.get_count)
            //                self.prepageButton.enabled=false;
            //            else
            //                self.prepageButton.enabled=true;
            //            if(self.offset+self.pageData.get_count>=self.recordCount)
            //                self.nextpageButton.enabled=false;
            //            else
            //                self.nextpageButton.enabled=true;
            //            [self.grid initgrid :self.pageData];
            
            
        });
    });
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//    [self.table beginUpdates];
    [self.table reloadData];
//    [self.table endUpdates];
    
}
//// 是否支持屏幕旋转
//- (BOOL)shouldAutorotate {
//
//
//    return YES;
//}
//// 支持的旋转方向
//- (NSUInteger)supportedInterfaceOrientations {
//    return UIInterfaceOrientationLandscapeRight;//UIInterfaceOrientationMaskAllButUpsideDown;
//}
//// 一开始的屏幕旋转方向
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationLandscapeRight;
//}



#pragma mark - web view delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if(navigationType != UIWebViewNavigationTypeLinkClicked)
        return true;
//    NSString *currentURL =[webView.request.URL lastPathComponent];// [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    NSString* str = request.URL.absoluteString;
    if([str isEqualToString:@"about:blank"])
        return true;
    WebViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
    ViewController.url = str;
    //    ViewController. = self.function_name;
    //    ViewController.behavior =BEHAVIOR_SEARCH;
    
    [self.navigationController pushViewController:ViewController animated:YES];
      DebugLog(@"shouldStartLoadWithRequest");
    return false;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{


      DebugLog(@"webViewDidFinishLoad @@@@@@@@@@@@@@@@@ entry");
    

   
    UITableViewCell *cell  = (UITableViewCell *)[[webView superview] superview];
    

    NSIndexPath *indexPath = [self.table indexPathForCell:cell];
    if(indexPath==nil)
    {//for ios7
        cell  = (UITableViewCell *) webView.superview.superview.superview;
        indexPath = [self.table indexPathForCell:cell];
    }
    
    float height= [[self.content.webviewHeight valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]] floatValue];
    
    if(height>0)
        return;
    
    
    //    UIScrollView *scrollerView = [webView.subviews objectAtIndex:0];
//    CGSize size= webView.scrollView.contentSize;
  //   CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    
    CGRect frame = webView.frame;
    NSString *fitHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
    frame.size.height = [fitHeight floatValue];
    //webView.frame = frame;
    
    
//      DebugLog(@"webViewDidFinishLoad ^^^^^^^^^^^^^^^^^Saveheight height=%f   section=%ld",webView.scrollView.contentSize.height,indexPath.section);
    
    [self.content.webviewHeight setObject:[NSString stringWithFormat:@"%f",frame.size.height/*webView.scrollView.contentSize.height*/] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]];
    
      DebugLog(@"webViewDidFinishLoad ^^^^^^^^^^^^^^^^^reloadRowsAtIndexPaths");

    

//    [self.table beginUpdates];
        [self.table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//    [self.table endUpdates];
    
//        [self.webviewoprationQueue addOperationWithBlock:^{
//    }];

    
//        [self.table endUpdates];
//    }

    
}

#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];
    NSString* type = [self.content get_segmenttype:indexPath.section];
    
    
    
    if([type isEqualToString:@"table"])
    {
        //            [self.content.webviewHeight setObject:[NSString stringWithFormat:@"%f",webView.frame.size.height] forKey:[NSString stringWithFormat:@"%d",webView.tag]];
        
        float height= [[self.content.webviewHeight valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]] floatValue];
        if(height==0)
            height=DEF_TABLE_HEIGHT;
        
        
//          DebugLog(@"heightForRowAtIndexPath...............webview,hight = %f  section=%ld",height,indexPath.section);
        //wait(1000);
        return height;
        
    }
    else if([type isEqualToString:@"mapping"])
    {
        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* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
        NSString* key =[item allKeys][0];
        //   [cell.keyLabel sizeToFit];
        NSString* val=[item valueForKey:key];
        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;
    }
    else
    {
          DebugLog(@"cellForRowAtIndexPath =====> list");
        
        float width = tableView.frame.size.width;
        width-=CELL_MARGIN*2;
        
        CGSize constraintval = CGSizeMake(width, 20000.0f);//val label width is 60% cell width;
        
        NSDictionary* item = self.content.segments[indexPath.section];
        
        NSString* text=[item valueForKey:[NSString stringWithFormat:@"line%ld",(long)indexPath.row]];
        
        
        
        if([text isEqual:[NSNull null]])
            text=@"";
        if(text==nil)
            text=@"";
        if([text isEqualToString:@"null"])
            text=@"";
        
        
        
        //        CGRect frame;
        //        frame.size = constraintval;
        //        frame.origin.x=0;
        //        frame.origin.y=0;
        
        
        CGSize sizeText = [text sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
        //           CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
        float height = sizeText.height;
        height = MAX(height+LINE_WIDTH, DEF_CELL_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, 0, 90, 22)];
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text =[self.content get_segmentname:section];
    [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
{
    
      DebugLog(@"numberOfSectionsInTableView=%ld",[self.content get_segmentcount]);
    return [self.content get_segmentcount];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      DebugLog(@"numberOfRowsInSection=%d",[self.content get_itemcount:section]);
    return [self.content get_itemcount:section];
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      DebugLog(@"titleForHeaderInSection=%@",[self.content get_segmentname:section]);
    return [self.content get_segmentname:section];
}
//- (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];
    


    NSString* type = [self.content get_segmenttype:indexPath.section];
    
    
    
    if([type isEqualToString:@"table"])
    {
          DebugLog(@"cellForRowAtIndexPath =====> table");
        
        static NSString *CellIdentifier = @"detail_item_web";
        
        DetailCellWeb *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
        NSString* tablecontent = [item valueForKey:@"content"];
        cell.webView.tag = indexPath.section;
        //        cell.webView.scrollView.contentSize.height = 0;
        cell.webView.scrollView.bounces=NO;
        cell.webView.scrollView.directionalLockEnabled = true;
        CGSize size= cell.webView.scrollView.contentSize;
        size.height=10;
        cell.webView.scrollView.contentSize=size;
        
        //        nsstring* c = [NSString stringwith]
        //       NSString *filePath = [[NSBundle mainBundle]pathForResource:@"about" ofType:@"htm"];
        //        NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        
        
        [cell.webView loadHTMLString:tablecontent baseURL:nil];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            wait(5000);
//              DebugLog(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!webview  section=%ld   height=%f",indexPath.section,cell.webView.scrollView.contentSize.height);
//           
////              DebugLog(@"url:%@", [self.webView stringByEvaluatingJavaScriptFromString:@"window.location.href;"]);       // shows google.com instead of about:blank
//        });
        
        
        
        //        CGRect rect = cell.webView.frame;
        //        [self.content set_itemheight:cell.webView.frame.size.height AtIndexPath:indexPath];
        //        [cell.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
        return cell;
    }
    else if([type isEqualToString:@"mapping"])
    {
        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* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
        NSString* key =[item allKeys][0];
        //   [cell.keyLabel sizeToFit];
        NSString* val=[item valueForKey:key];
        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];
//          DebugLog(@"tableView width:%f",width);
//        
//          DebugLog(@"key width= %f val width = %f",constraintkey.width,constraintval.width);
//          DebugLog(@"key = %@ val = %@",key,val);
        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";
        DetailCellKV *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if(cell==nil)
              DebugLog(@"cell is nil.........................");
        
        
        //        CGRect framekey ;
        //        framekey.origin.x=CELL_MARGIN;
        //        framekey.origin.y=LINE_WIDTH;
        //        framekey.size = sizekey;
        //        UILabel* keyLabel = [[UILabel alloc]initWithFrame:framekey];
        //        keyLabel.lineBreakMode = NSLineBreakByWordWrapping;
        //        keyLabel.backgroundColor = [UIColor grayColor];
        //        keyLabel.text = key;
        //
        //        CGRect frameval;
        //        frameval.origin.x=constraintkey.width+CELL_MARGIN;
        //        frameval.origin.y=LINE_WIDTH;
        //        frameval.size = sizeval;
        //
        //        UILabel* valLabel = [[UILabel alloc]initWithFrame:frameval];
        //         valLabel.lineBreakMode = NSLineBreakByWordWrapping;
        //        valLabel.text = val;
        //
        //        for(UIView* v in cell.contentView.subviews)
        //        {
        //            [v removeFromSuperview];
        //
        //        }
        //
        //        [cell.contentView addSubview:keyLabel];
        //        [cell.contentView addSubview:valLabel];
        
        
        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了.

        
        
        //   [cell.valLabel sizeToFit];
        //        cell.anameLabel.text = [field valueForKey:@"aname"];
        //        [cell.anameLabel sizeToFit];
        //          DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
        // Configure the cell...
        //        CGRect rect = cell.frame;
        //        [self.content set_itemheight:100 AtIndexPath:indexPath];
        return cell;
    }
    else
    {
          DebugLog(@"cellForRowAtIndexPath =====> list");
        
        static NSString *CellIdentifier = @"detail_item_list";
        
        
        
        float width = tableView.frame.size.width;
        
        width-=CELL_MARGIN*2;
        CGSize constraintkey = CGSizeMake(width, 20000.0f);//key label width is 40% cell width;
        
        NSDictionary* item = self.content.segments[indexPath.section];
        
        NSString* val=[item valueForKey:[NSString stringWithFormat:@"line%ld",(long)indexPath.row]];
        
        if([val isEqual:[NSNull null]])
            val=@"";
        if(val==nil)
            val=@"";
        if([val isEqualToString:@"null"])
            val=@"";
        
        
        
        
        
        CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
        
        
        float height = sizeval.height;
        height = MAX(height, DEF_CELL_HEIGHT-LINE_WIDTH);
        sizeval.height = height;
        sizeval.width = constraintkey.width;
        
        
        
        
        
        
        
        
        //创建cell
        DetailCellList *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        CGRect framekey = cell.listLabel.frame;
        framekey.origin.x=CELL_MARGIN;
        framekey.origin.y=LINE_WIDTH;
        framekey.size = sizeval;
        cell.listLabel.frame = framekey;
        cell.listLabel.text= val;
        
        
        
        return cell;
        
    }
    return nil;
}

@end
