我从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];
}
`
您每次都在单元格的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] 删除。
我来说两句