uiscrollview加载TWRChartView时,只加载了第一个图表的问题 5C

我在viewdidload里面做了数据请求
并在数据请求的成功方法中调用了页面的初始化方法[self chartViewSubView];
在这个方法中初始化了5个TWRChartView,并添加到一个scrollview当中
然后调用[self myLoadLineChart],将五组数据生成TWRLineChart,并loadLineChart
但是当程序第一次启动后进入到这个页面的时候,只加载了第一个chartview的内容,scrollview滚动的时候,第2-5页都是空的。退出这个页面再进入的时候就能够全部显示了。

现在我只能延迟0.5秒后再执行一遍[self myLoadLineChart],并且让scrollview刷新,才能够在第一次进入的时候全部加载。

这是什么问题?求一个详细一点的解答

数据请求成功部分的代码
[Network networkPOSTRequestWithURL:WORKPLANER body:bodys pageUniquenessIdentifier:bodys result:^(id result) {
self.workDic = [[NSDictionary alloc]init];
self.workDic = [result objectForKey:@"output"];

[self scrollSubView];//加载scrollview
[self chartViewSubView];//加载5个TWRChartView
//[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(myLoadLineChart) userInfo:nil repeats:NO]; //刷新方法,注释掉后只显示第一个图表
[self.HUD hideAnimated:YES];
}

加载chartview的代码

  • (void)chartViewSubView{
    NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"js"];

    UIView *backview1 = [[UIView alloc]initWithFrame:CGRectMake(10, 40, WIDTH - 20, HEIGHT - 370)];
    backview1.layer.cornerRadius = 10;
    backview1.layer.masksToBounds = YES;
    backview1.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backview1.layer.borderWidth = 1;
    backview1.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:backview1];

    _chartView1 = [[TWRChartView alloc] initWithFrame:CGRectMake(15, 45, WIDTH - 30, HEIGHT - 380)];
    _chartView1.backgroundColor = [UIColor whiteColor];
    [_chartView1 setChartJsFilePath:jsFilePath];
    [self.scroll addSubview:_chartView1];

    UIView *backview2 = [[UIView alloc]initWithFrame:CGRectMake(10 + WIDTH, 40, WIDTH - 20, HEIGHT - 370)];
    backview2.layer.cornerRadius = 10;
    backview2.layer.masksToBounds = YES;
    backview2.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backview2.layer.borderWidth = 1;
    backview2.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:backview2];

    _chartView2 = [[TWRChartView alloc] initWithFrame:CGRectMake(WIDTH + 15, 45, WIDTH - 30, HEIGHT - 380)];
    _chartView2.backgroundColor = [UIColor whiteColor];
    [_chartView2 setChartJsFilePath:jsFilePath];
    [self.scroll addSubview:_chartView2];

    UIView *backview3 = [[UIView alloc]initWithFrame:CGRectMake(10 + WIDTH * 2, 40, WIDTH - 20, HEIGHT - 370)];
    backview3.layer.cornerRadius = 10;
    backview3.layer.masksToBounds = YES;
    backview3.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backview3.layer.borderWidth = 1;
    backview3.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:backview3];

    _chartView3 = [[TWRChartView alloc] initWithFrame:CGRectMake(WIDTH * 2 + 15, 45, WIDTH - 30, HEIGHT - 380)];
    _chartView3.backgroundColor = [UIColor whiteColor];
    [_chartView3 setChartJsFilePath:jsFilePath];
    [self.scroll addSubview:_chartView3];

    UIView *backview4 = [[UIView alloc]initWithFrame:CGRectMake(10 + WIDTH * 3, 40, WIDTH - 20, HEIGHT - 370)];
    backview4.layer.cornerRadius = 10;
    backview4.layer.masksToBounds = YES;
    backview4.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backview4.layer.borderWidth = 1;
    backview4.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:backview4];

    _chartView4 = [[TWRChartView alloc] initWithFrame:CGRectMake(WIDTH * 3 + 15, 45, WIDTH - 30, HEIGHT - 380)];
    _chartView4.backgroundColor = [UIColor whiteColor];
    [_chartView4 setChartJsFilePath:jsFilePath];
    [self.scroll addSubview:_chartView4];

    UIView *backview5 = [[UIView alloc]initWithFrame:CGRectMake(10 + WIDTH * 4, 40, WIDTH - 20, HEIGHT - 370)];
    backview5.layer.cornerRadius = 10;
    backview5.layer.masksToBounds = YES;
    backview5.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backview5.layer.borderWidth = 1;
    backview5.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:backview5];

    _chartView5 = [[TWRChartView alloc] initWithFrame:CGRectMake(WIDTH * 4 + 15, 45, WIDTH - 30, HEIGHT - 380)];
    _chartView5.backgroundColor = [UIColor whiteColor];
    [_chartView5 setChartJsFilePath:jsFilePath];
    [self.scroll addSubview:_chartView5];

    [self myLoadLineChart];//在这里调用加载TWRLineChart的方法
    }

加载TWRLineChart的方法

  • (void)myLoadLineChart {
    NSArray *listArr = @[@"dayList",@"oilkmList",@"mileList",@"oilList",@"otherList"];

    for (int t = 0; t < listArr.count; t++) {
    NSMutableArray *arr1 = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartData1"];
    NSMutableArray *arr2 = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartData2"];
    NSMutableArray *arr3 = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartData3"];
    NSMutableArray *arr4 = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartData4"];
    NSMutableArray *arr5 = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartData5"];
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    arr = [[self.workDic objectForKey:listArr[t]] objectForKey:@"chartDate"];

    //预设颜色
    UIColor *strokeColor1 = [UIColor orangeColor];
    UIColor *strokeColor2 = [UIColor redColor];
    UIColor *strokeColor3 = [UIColor blueColor];
    UIColor *strokeColor4 = [UIColor purpleColor];
    UIColor *strokeColor5 = [UIColor greenColor];
    
    //当数据为单条时,添加单个数据
    if ([arr count] == 1) {
        [arr insertObject:@"" atIndex:0];
        [arr1 insertObject:@"0" atIndex:0];
        [arr2 insertObject:@"0" atIndex:0];
        [arr3 insertObject:@"0" atIndex:0];
        [arr4 insertObject:@"0" atIndex:0];
        [arr5 insertObject:@"0" atIndex:0];
    }
    
    if ([arr count] > 0 && [[arr objectAtIndex:0] isEqualToString:@""]) {
        //当单条数据时,线路颜色设为无色
        strokeColor1 = [UIColor clearColor];
        strokeColor2 = [UIColor clearColor];
        strokeColor3 = [UIColor clearColor];
        strokeColor4 = [UIColor clearColor];
        strokeColor5 = [UIColor clearColor];
    }
    
    TWRDataSet *dataSet1 = [[TWRDataSet alloc] initWithDataPoints:arr1 fillColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0] strokeColor:strokeColor1 pointColor:[UIColor orangeColor] pointStrokeColor:[UIColor orangeColor]];
    TWRDataSet *dataSet2 = [[TWRDataSet alloc] initWithDataPoints:arr2 fillColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0] strokeColor:strokeColor2 pointColor:[UIColor redColor] pointStrokeColor:[UIColor redColor]];
    TWRDataSet *dataSet3 = [[TWRDataSet alloc] initWithDataPoints:arr3 fillColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0] strokeColor:strokeColor3 pointColor:[UIColor blueColor] pointStrokeColor:[UIColor blueColor]];
    TWRDataSet *dataSet4 = [[TWRDataSet alloc] initWithDataPoints:arr4 fillColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0] strokeColor:strokeColor4 pointColor:[UIColor purpleColor] pointStrokeColor:[UIColor purpleColor]];
    TWRDataSet *dataSet5 = [[TWRDataSet alloc] initWithDataPoints:arr5 fillColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0] strokeColor:strokeColor5 pointColor:[UIColor greenColor] pointStrokeColor:[UIColor greenColor]];
    
    NSArray * dataD = @[arr1, arr2, arr3, arr4, arr5];
    NSArray * data = @[dataSet1, dataSet2, dataSet3, dataSet4, dataSet5];
    NSMutableArray *muArr = [[NSMutableArray alloc]init];
    for (int i = 0; i < dataD.count; i++) {
        if ([dataD[i] count] != 0) {
            [muArr addObject:data[i]];
        }
    }
    
    NSArray *labels = arr;
    
    TWRLineChart *line = [[TWRLineChart alloc] initWithLabels:labels dataSets:muArr animated:NO curved:NO];
    // Load data
    if (t == 0) {
        [_chartView1 loadLineChart:line];
    }else if(t == 1){
        [_chartView2 loadLineChart:line];
    }else if(t == 2){
        [_chartView3 loadLineChart:line];
    }else if(t == 3){
        [_chartView4 loadLineChart:line];
    }else if(t == 4){
        [_chartView5 loadLineChart:line];
    }
    

    }
    //[self.scroll reloadInputViews];
    }

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐