Я делаю две пользовательские ячейки и отображаю их в одном UITableView в нечетных четных порядках, но когда я прокручиваю UITableView, все изображения и цвет данных расписания будут меняться случайным образом, и будут отображаться дубликаты данных, но имя, exp. , pri-zone и скорость, которая находится в строке, все они отображаются правильно, изменяется только изображение и цвет текста расписания.ios tableview image load duplicate when scrolling tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
static NSString *[email protected]"MyCustomCell";
TableViewCell *cell=[tblResponseData dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier];
}
NSString *imgPath=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Pic"];
// NSString *imgPath=[[appDelegate.arrData
objectAtIndex:indexPath.row]valueForKey:@"image"];
NSURL *url = [NSURL URLWithString:[imgPath
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *schedule =[[arrData objectAtIndex:indexPath.row]valueForKey:@"Schedule"];
NSArray *strings = [schedule componentsSeparatedByString:@","];
//NSURL *url = [NSURL URLWithString:url];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url]
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse * resp, NSData * data, NSError
* error)
{
if(error == NULL){
// here is our image
UIImage *image = [UIImage imageWithData:data];
cell.imgThumbnail.image=image;
}
else{
cell.imgThumbnail.image=[UIImage imageNamed:@"no preview 2.jpeg"];
}
}];
for (NSString *item in strings)
{
if ([item isEqualToString:@"Su"]) {
cell.lblsu.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Tu"]){
cell.lblt.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Th"]){
cell.lblth.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"M"]){
cell.lblm.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"F"]){
cell.lblf.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"S"]){
cell.lbls.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"W"]){
cell.lblw.textColor = [UIColor greenColor];
}
}
cell.lblname.text = [[arrData objectAtIndex:indexPath.row] valueForKey:@"Worker Name"];
cell.lblexperience.text=[[arrData o
bjectAtIndex:indexPath.row]valueForKey:@"Experience"];
cell.lblprizone.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Primary
Zone"];
cell.lblrate.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Rate"];
return cell;
} else {
static NSString *[email protected]"MyCustomCell1";
TableViewCell *cell1=[tblResponseData d
equeueReusableCellWithIdentifier:cellIdentifier1];
if (cell1 == nil)
{
cell1 = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier1];
}
NSString *imgPath=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Pic"];
// NSString *imgPath=[[appDelegate.arrData
objectAtIndex:indexPath.row]valueForKey:@"image"];
NSURL *url = [NSURL URLWithString:[imgPath
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSURL *url = [NSURL URLWithString:url];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url]
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse * resp, NSData * data, NSError
* error)
{
if(error == NULL){
// here is our image
UIImage *image = [UIImage imageWithData:data];
cell1.imgThumbnail.image=image;
}
else{
cell1.imgThumbnail.image=[UIImage imageNamed:@"no preview 2.jpeg"];
}
}];
// NSURL *imageUrl = [NSURL URLWithString:imgPath];
// NSData *imageData = [NSData dataWithContentsOfURL:url];
// UIImage *image=[[UIImage alloc]initWithData:imageData];
// cell.imgThumbnail.image=image;
NSString *schedule =[[arrData objectAtIndex:indexPath.row]valueForKey:@"Schedule"];
NSArray *strings = [schedule componentsSeparatedByString:@","];
for (NSString *item in strings)
{
if ([item isEqualToString:@"Su"]) {
cell1.lblsu.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Tu"]){
cell1.lblt.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Th"]){
cell1.lblth.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"M"]){
cell1.lblm.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"F"]){
cell1.lblf.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"S"]){
cell1.lbls.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"W"]){
cell1.lblw.textColor = [UIColor greenColor];
}
}
cell1.lblname.text = [[arrData objectAtIndex:indexPath.row] valueForKey:@"Worker N
ame"];
cell1.lblexperience.text=[[arrData
objectAtIndex:indexPath.row]valueForKey:@"Experience"];
cell1.lblprizone.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Primary
Zone"];
cell1.lblrate.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Rate"];
return cell1;
}
}