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

妮莎·古普塔(Nisha Gupta)

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



}
    `
玛雅克·贾恩(Mayank Jain)

您每次都在单元格的contentview上添加子视图。仅当单元格为nil时,才应添加子视图。修改此方法

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么 npm install 命令在 node app 中一次又一次地重新运行?

在 Fortran 95 中一次又一次地读取文件的内容

init方法在servlet中一次又一次地调用

为什么数据在 Firebase 数据库的 Loop 中一次又一次地添加?

经过身份验证的密钥斗篷后,页面一次又一次地重新加载

我的列表视图应用程序一次又一次地崩溃

避免一次又一次地从JSON获取数据

Flutter:为什么setState((){})一次又一次地设置数据

更新状态一次又一次地获取数据后

我需要一次又一次刷新dns以使站点加载

在 html 中一次又一次地浏览同一个文件

同一对象在C#中一次又一次地实例化

如何在JavaScript中一次又一次地获得翻转效果?

如何停止在 JavaScript 中一次又一次地调用相同的函数?

如何防止一次又一次地重新创建新的Fragment?

以编程方式一次又一次地关闭SwiftUI视图不起作用

我希望我的程序一次又一次地运行而不会停止

CoreData将数据一次又一次地添加到数据库中

通知被一次又一次地触发

如何一次又一次地调用URL

Azure 容器实例一次又一次地失败

一次又一次地馈送avconv

内核一次又一次地死亡

一次又一次地编辑python脚本文件

无法一次又一次地在LinkedList中插入相同的元素

Square 一次又一次地改变速度

如何一次又一次地重复(递归)查询?

要重用jQuery函数,使函数一次又一次地使用

如何一次又一次地选择读/写?