我的表视图的数据一次又一次重装? [英] My Data in table view reloaded again and again?

查看:104
本文介绍了我的表视图的数据一次又一次重装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JSON服务器的响应,并表明对表视图数据,但是当我向下滚动数据将再次重新显示上重叠每个单元。谁能告诉我,为什么会这样呢?我的code是below`

  self.title = @表数据;    如果(检查)
    {
        self.title = @第二页;
    }
    [自送出数据]    deliveryCases = [[UITableView的页头] initWithFrame:方法CGRectMake(0,0,320,480)];
    deliveryCases.backgroundColor =的UIColor clearColor]
    deliveryCases.dataSource =自我;
    deliveryCases.delegate =自我;
    [self.view addSubview:deliveryCases];    myCaseName = [[NSArray的页头]初始化];
- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    静态的NSString * cellIdentifier = @CellIdentifier
    * UITableViewCell的细胞= [deliveryCases dequeueReusableCellWithIdentifier:cellIdentifier];    //显示在表视图中的数据    D = [deliveryCaseArray objectAtIndex:indexPath.row];    如果(细胞==零)
    {
        电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }    myCaseName = d.caseName;    *的UILabel = caseNameLbl [的UILabel页头] initWithFrame:方法CGRectMake(70,10,100,50);
    caseNameLbl.text = [的NSString stringWithFormat:@%@,myCaseName]
    caseNameLbl.textColor =的UIColor blackColor]
    UIFont * boldFontlblDate = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseNameLbl并入setfont:boldFontlblDate];
    caseNameLbl.backgroundColor =的UIColor clearColor]
    [cell.contentView addSubview:caseNameLbl];    *的UILabel = caseDateLbl [的UILabel页头] initWithFrame:方法CGRectMake(160,10,100,50);
    NSDateFormatter *格式化= [[NSDateFormatter的alloc]初始化];
    formatter.dateFormat = @M / D / YYYY;
    的NSString * dateString = [格式化stringFromDate:[NSDate的日期]];
    d.date = dateString;
    caseDateLbl.text = [的NSString stringWithFormat:@%@,dateString]
    caseDateLbl.textColor =的UIColor blackColor]
    UIFont * boldFontlblStatus = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseDateLbl并入setfont:boldFontlblStatus];
    caseDateLbl.backgroundColor =的UIColor clearColor]
    [cell.contentView addSubview:caseDateLbl];    *的UILabel = caseStatusLbl [的UILabel页头] initWithFrame:方法CGRectMake(250,10,100,50);
    caseStatusLbl.text = [的NSString stringWithFormat:@%@,d.status]
    caseStatusLbl.textColor =的UIColor blackColor]
    UIFont * boldFontlblTrack = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseStatusLbl并入setfont:boldFontlblTrack];
    caseStatusLbl.backgroundColor =的UIColor clearColor]
    [cell.contentView addSubview:caseStatusLbl];    [单元setUserInteractionEnabled:YES];
    tableView.allowsSelection = YES;    返回细胞;} - (NSInteger的)numberOfSectionsInTableView:(UITableView的*)tableView3
{
    返回1;
} - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分
{
    返回deliveryCaseArray.count;
} - (CGFloat的)的tableView:(UITableView的*)的tableView heightForHeaderInSection:(NSInteger的)部分
{
    返回20.0;
} - (UIView的*)的tableView:(UITableView的*)的tableView viewForHeaderInSection:(NSInteger的)部分
{    的UIView * headerView = [[UIView的的alloc] initWithFrame:方法CGRectMake(0,0,350,10)];    *的UILabel = LBL1 [的UILabel页头] initWithFrame:方法CGRectMake(10,0,100,50);
    lbl1.text = @#;
    [headerView addSubview:LBL1];    *的UILabel LBL2 = [[的UILabel页头] initWithFrame:方法CGRectMake(70,0,100,50);
    lbl2.text = @案例#;
    [headerView addSubview:LBL2];    *的UILabel = lbl3 [的UILabel页头] initWithFrame:方法CGRectMake(160,0,100,50);
    lbl3.text = @日期;
    [headerView addSubview:lbl3];    *的UILabel = lbl4 [的UILabel页头] initWithFrame:方法CGRectMake(220,0,100,50);
    lbl4.text = @状态类型;
    [headerView addSubview:lbl4];    返回headerView;} - (无效)送出数据
{
        * NSUserDefaults的uidSave = [NSUserDefaults的standardUserDefaults]
        的NSMutableDictionary *获取= [[的NSMutableDictionary的alloc]初始化];
        [拿到的setObject:[uidSave valueForKey:@UID] forKey:@UID];
        [拿到的setObject:@开放forKey:@型];
        的NSLog(@字典数据是要拿到%@,获得);
        NSData的* jsonData = [NSJSONSerialization dataWithJSONObject:获取选项:kNilOptions错误:无];
        * NSString的jsonInputString = [[NSString的页头] initWithData:jsonData编码:NSUTF8StringEncoding];
        * NSString的后期= [[NSString的页头] initWithFormat:@R =%@,jsonInputString]        NSURL * URL = [NSURL URLWithString:[的NSString stringWithFormat:@%@,caseTypeUrl]];        NSData的* POSTDATA = [交dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        * NSString的postLength = [的NSString stringWithFormat:@%吕氏春秋,(无符号长)POSTDATA长度]];
        NSMutableURLRequest *请求= [NSMutableURLRequest requestWithURL:URL的CachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0;
        [要求setURL:URL]
        [要求setHTTPMethod:@POST];
        [申请的setValue:postLength forHTTPHeaderField:@内容长度];
        [申请的setValue:@应用程序/ x-WWW的形式urlen codeDforHTTPHeaderField:@的Content-Type];
        [要求setHTTPBody:POSTDATA];        NSError *错误;
        NSURLResponse *响应;
        NSData的* responseData = [NSURLConnection的sendSynchronousRequest:请求returningResponse:安培;响应错误:放大器;错误]
        如果(responseData!=无)
        {            jsonArray = [NSJSONSerialization JSONObjectWithData:responseData选项:kNilOptions错误:&放大器;错误]
            的NSLog(@价值观=======%@,JSONArray),其中;
        }        如果(错误)
        {
            的NSLog(@错误%@,error.description);
            UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@错误消息:@服务器没有响应委托:自我cancelButtonTitle:@取消otherButtonTitles:无,无]
            [alertView秀]
        }
    //设置名称数组
   deliveryCaseArray = [[NSMutableArray里的alloc]初始化];    //循环通过我们的JSON数组
    的for(int i = 0; I< jsonArray.count;我++)
    {
        //创建对象
        * NSString的DATE1 = [[jsonArray objectAtIndex:我] objectForKey:@日期];
        * NSString的protracking2 = [[jsonArray objectAtIndex:我] objectForKey:@protracking];
        * NSString的STATUS3 =​​ [[jsonArray objectAtIndex:我] objectForKey:@状态];        [deliveryCaseArray ADDOBJECT:[数据对象页头] initWithDate:日期1 andCaseName:protracking2和STATUS:STATUS3]];    }    [deliveryCases reloadData];}
    `


解决方案

您正在对细胞的内容查看每次加子视图。只有当你的细胞是零,你应该添加子视图。修改此法

   - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    静态的NSString * cellIdentifier = @CellIdentifier
    * UITableViewCell的细胞= [deliveryCases dequeueReusableCellWithIdentifier:cellIdentifier];//显示在表视图中的数据    D = [deliveryCaseArray objectAtIndex:indexPath.row];
    myCaseName = d.caseName;
    如果(细胞==零)
    {
        电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];        *的UILabel = caseNameLbl [的UILabel页头] initWithFrame:方法CGRectMake(70,10,100,50);
        caseNameLbl.text = [的NSString stringWithFormat:@%@,myCaseName]
        caseNameLbl.textColor =的UIColor blackColor]
        UIFont * boldFontlblDate = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
        [caseNameLbl并入setfont:boldFontlblDate];
        caseNameLbl.backgroundColor =的UIColor clearColor]
        [cell.contentView addSubview:caseNameLbl];        *的UILabel = caseDateLbl [的UILabel页头] initWithFrame:方法CGRectMake(160,10,100,50);
        NSDateFormatter *格式化= [[NSDateFormatter的alloc]初始化];
        formatter.dateFormat = @M / D / YYYY;
        的NSString * dateString = [格式化stringFromDate:[NSDate的日期]];
        d.date = dateString;
        caseDateLbl.text = [的NSString stringWithFormat:@%@,dateString]
        caseDateLbl.textColor =的UIColor blackColor]
        UIFont * boldFontlblStatus = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
       [caseDateLbl并入setfont:boldFontlblStatus];
       caseDateLbl.backgroundColor =的UIColor clearColor]
       [cell.contentView addSubview:caseDateLbl];       *的UILabel = caseStatusLbl [的UILabel页头] initWithFrame:方法CGRectMake(250,10,100,50);
       caseStatusLbl.text = [的NSString stringWithFormat:@%@,d.status]
       caseStatusLbl.textColor =的UIColor blackColor]
       UIFont * boldFontlblTrack = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
       [caseStatusLbl并入setfont:boldFontlblTrack];
       caseStatusLbl.backgroundColor =的UIColor clearColor]
       [cell.contentView addSubview:caseStatusLbl];
   }
[单元setUserInteractionEnabled:YES];
tableView.allowsSelection = YES;
返回细胞;
}

I am getting the response from json server and show that data on table view but when i scroll down the data will show again again overlaps on each cell. Anyone can tell me why this happening?My Code is below`

self.title=@"Table Data";

    if(check)
    {
        self.title = @"Second Page";
    }
    [self sendData];

    deliveryCases = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 320, 480)];
    deliveryCases.backgroundColor = [UIColor clearColor];
    deliveryCases.dataSource = self;
    deliveryCases.delegate = self;
    [self.view addSubview:deliveryCases];

    myCaseName = [[NSArray alloc]init];
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [deliveryCases dequeueReusableCellWithIdentifier:cellIdentifier];

    //    Showing data in table view

    d = [deliveryCaseArray objectAtIndex:indexPath.row];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    myCaseName = d.caseName;

    UILabel *caseNameLbl =[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100 ,50)];
    caseNameLbl.text = [NSString stringWithFormat:@"%@",myCaseName];
    caseNameLbl.textColor=[UIColor blackColor];
    UIFont * boldFontlblDate = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseNameLbl setFont:boldFontlblDate];
    caseNameLbl.backgroundColor =[UIColor clearColor];
    [cell.contentView addSubview:caseNameLbl];

    UILabel *caseDateLbl =[[UILabel alloc]initWithFrame:CGRectMake(160, 10, 100 ,50)];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"M/d/yyyy";
    NSString *dateString =[formatter stringFromDate:[NSDate date]];
    d.date = dateString;
    caseDateLbl.text = [NSString stringWithFormat:@"%@", dateString];
    caseDateLbl.textColor=[UIColor blackColor];
    UIFont * boldFontlblStatus= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseDateLbl setFont:boldFontlblStatus];
    caseDateLbl.backgroundColor =[UIColor clearColor];
    [cell.contentView addSubview:caseDateLbl];

    UILabel *caseStatusLbl =[[UILabel alloc]initWithFrame:CGRectMake(250, 10, 100 ,50)];
    caseStatusLbl.text = [NSString stringWithFormat:@"%@", d.status];
    caseStatusLbl.textColor=[UIColor blackColor];
    UIFont * boldFontlblTrack= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    [caseStatusLbl setFont:boldFontlblTrack];
    caseStatusLbl.backgroundColor =[UIColor clearColor];
    [cell.contentView addSubview:caseStatusLbl];

    [cell setUserInteractionEnabled:YES];
    tableView.allowsSelection=YES;

    return cell;

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView3
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  deliveryCaseArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.0;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350 , 10)];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100 , 50)];
    lbl1.text = @"#";
    [headerView addSubview:lbl1];

    UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 100, 50)];
    lbl2.text = @"Case #";
    [headerView addSubview:lbl2];

    UILabel *lbl3 = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 100, 50)];
    lbl3.text = @"Date";
    [headerView addSubview:lbl3];

    UILabel *lbl4 = [[UILabel alloc] initWithFrame:CGRectMake(220, 0, 100, 50)];
    lbl4.text = @"Status Type";
    [headerView addSubview:lbl4];

    return headerView;

}

-(void)sendData
{
        NSUserDefaults *uidSave = [NSUserDefaults standardUserDefaults];
        NSMutableDictionary *get = [[NSMutableDictionary alloc]init];
        [get setObject:[uidSave valueForKey:@"uid"]forKey:@"uid"];
        [get setObject:@"open" forKey:@"type"];
        NSLog(@"Dictionary Data which is to be get %@",get);
        NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
        NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];

        NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",caseTypeUrl]];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        NSError *error;
        NSURLResponse *response;
        NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if (responseData != nil)
        {

            jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
            NSLog(@"Values =======%@",jsonArray);


        }

        if (error)
        {
            NSLog(@"error %@",error.description);
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Server not responding" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
            [alertView show];
        }


    // Set up names array
   deliveryCaseArray = [[NSMutableArray alloc]init];

    // Loop through our json Array
    for (int i = 0 ; i <jsonArray.count; i++)
    {
        //create object
        NSString *date1 = [[jsonArray objectAtIndex:i]objectForKey:@"date"];
        NSString *protracking2 = [[jsonArray objectAtIndex:i]objectForKey:@"protracking"];
        NSString *status3 = [[jsonArray objectAtIndex:i]objectForKey:@"status"];

        [deliveryCaseArray addObject:[[DataObjects alloc]initWithDate:date1 andCaseName:protracking2 andStatus:status3]];

    }

    [deliveryCases reloadData];



}
    `

解决方案

You are adding subview on cell's contentview each time. You should add subview only when your cell is nil. Modify this method

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [deliveryCases dequeueReusableCellWithIdentifier:cellIdentifier];

//    Showing data in table view

    d = [deliveryCaseArray objectAtIndex:indexPath.row];
    myCaseName = d.caseName;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        UILabel *caseNameLbl =[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100 ,50)];
        caseNameLbl.text = [NSString stringWithFormat:@"%@",myCaseName];
        caseNameLbl.textColor=[UIColor blackColor];
        UIFont * boldFontlblDate = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
        [caseNameLbl setFont:boldFontlblDate];
        caseNameLbl.backgroundColor =[UIColor clearColor];
        [cell.contentView addSubview:caseNameLbl];

        UILabel *caseDateLbl =[[UILabel alloc]initWithFrame:CGRectMake(160, 10, 100 ,50)];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"M/d/yyyy";
        NSString *dateString =[formatter stringFromDate:[NSDate date]];
        d.date = dateString;
        caseDateLbl.text = [NSString stringWithFormat:@"%@", dateString];
        caseDateLbl.textColor=[UIColor blackColor];
        UIFont * boldFontlblStatus= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
       [caseDateLbl setFont:boldFontlblStatus];
       caseDateLbl.backgroundColor =[UIColor clearColor];
       [cell.contentView addSubview:caseDateLbl];

       UILabel *caseStatusLbl =[[UILabel alloc]initWithFrame:CGRectMake(250, 10, 100 ,50)];
       caseStatusLbl.text = [NSString stringWithFormat:@"%@", d.status];
       caseStatusLbl.textColor=[UIColor blackColor];
       UIFont * boldFontlblTrack= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
       [caseStatusLbl setFont:boldFontlblTrack];
       caseStatusLbl.backgroundColor =[UIColor clearColor];
       [cell.contentView addSubview:caseStatusLbl];
   }
[cell setUserInteractionEnabled:YES];
tableView.allowsSelection=YES;
return cell;
}

这篇关于我的表视图的数据一次又一次重装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆