表格视图中的我的数据一次又一次地重新加载?

时间:2015-07-06 06:09:21

标签: ios objective-c arrays

我从json服务器获得响应并在表视图上显示数据,但是当我向下滚动数据时,将再次显示每个单元格上的重叠。任何人都可以告诉我为什么会这样?我的代码在下面?

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];



}
    `

3 个答案:

答案 0 :(得分:2)

您每次都会在小区的内容视图中添加子视图。只有在单元格为零时才应添加子视图。修改此方法

-(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;
}

答案 1 :(得分:1)

因为这个

UILabel *caseNameLbl =[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100 ,50)];
[cell.contentView addSubview:caseNameLbl];
UILabel *caseDateLbl =[[UILabel alloc]initWithFrame:CGRectMake(160, 10, 100 ,50)];
UILabel *caseStatusLbl =[[UILabel alloc]initWithFrame:CGRectMake(250, 10, 100 ,50)];

您每次调用cellForRowAtIndexpath时都会分配标签并添加标签。

答案 2 :(得分:-2)

在重复使用时,您会在单元格中反复添加子视图。因此,当第一个单元格被重用时,它已经具有之前的标签,并且还添加了新标签。要摆脱它们,您必须在初始化单元格后添加它。

NSArray *subviewArray = cell.contentView.subviews;
for (UIView *view in subviewArray) {
    [view removeFromSuperview];
}

这将删除较旧的子视图,以便只显示新的子视图。

<强>更新

如其他答案所述,上述代码并非最佳方法。最佳和最有效的方法是使用如下界面创建UITableViewCell类:

@interface CustomTableViewCell : UITableViewCell

@property (weak,nonatomic) UILabel *caseNameLbl;
@property (weak,nonatomic) UILabel *caseDateLbl;
@property (weak,nonatomic) UILabel *caseStatusLbl;

@end

然后在cellForRowAtIndexPath:方法中相应地填充单元格。