我在单个UITableView中制作两个自定义单元格并显示奇数偶数顺序,但是当我滚动UITableView时,所有图像和计划数据颜色将随机变化,并且将显示重复数据,但名称,exp。,pri-zone和rate在字符串中它们都显示正确,只有日程文本的图像和颜色会改变。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
static NSString *cellIdentifier=@"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 *cellIdentifier1=@"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;
}
}
答案 0 :(得分:0)
我认为可能是因为您按NSURLConnection sendAsynchronousRequest
加载了图片。因为completitionhandler不是立即调用的,而是在将数据返回给请求时。因此,在加载单元格之后,图像将被设置为。
尝试替换
[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"];
}
}];
使用代码将imgThumbnail.image
设置为项目中的图像,并查看是否仍然可以更改单元格。
答案 1 :(得分:0)
问题:您正在使用dequeueReusableCellWithIdentifier,因此在滚动tableview时会重复使用相同的单元格,并且您在该单元格中发送AsynchronousRequest,以便我们在另一个单元格上接收先前发送的单元格响应,以便它显示出不同的结果。
例如:您发送请求的单元格2,然后您滚动表格视图,以便在单元格10重新使用相同的单元格时,您将收到单元格2发送请求的响应该图像(响应数据)显示在单元格10中。
解决方案:以单独的方法发送请求并将图像保存在数组或字典中,然后根据收到的响应重新加载相应的单元格。
例如: EGOImageView正在异步发送请求并保存在缓存中并在收到响应后显示请检查一次
答案 2 :(得分:0)
尝试这个,用于从webservices加载图像。
NSString *imgUrl = [[arrCategoryDetails objectAtIndex:indexPath.row]valueForKey:@"Image"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
if (imgData) {
UIImage *image = [UIImage imageWithData:imgData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
CategoryDetailsTableCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
updateCell.imgForCategoryDetailCell.image = image;
});
}
}
});