2014-09-13 165 views
0

我做了两个自定义单元格,并显示在单一的UITableView奇偶命令,但是当我滚动UITableView所有图像和时间表数据颜色将随机更改和重复数据将显示,但名称,exp。 ,pri-zone和字符串中的速率都显示正确,只有日程文本的图像和颜色会发生变化。ios tableview图像加载时重复滚动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; 
    } 

     } 

回答

0

我想这可能是因为你加载你的图片NSURLConnection sendAsynchronousRequest。因为Completitionhandler不是立即调用的,而是当数据返回到请求时。因此,在装载了单元格的之后,图像将被设置为。 尝试用代码,将设置imgThumbnail.image到在你的项目中的图像,看看你是否仍然得到radomly改变的细胞来代替

[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"]; 
     } 
    }]; 

0

问题:您正在使用dequeueReusableCellWithIdentifier所以相同的细胞被重用,当你滚动tableview中,你在该小区发送AsynchronousRequest所以先前发送的细胞反应,我们是在另一个单元接收所以也呈现不同结果。

例如:小区2正在发送请求,然后你滚动表视图,以便相同的小区是由时间重新用于电池10将收到单元2发送请求的响应,并将该图像(响应数据)被显示在电池10

解决方案:发送在单独的方法中的请求,并且将图像保存阵列或字典然后重新加载基于所接收到的响应对应的细胞。

例如:EGOImageView正在接收响应之后异步地发送请求和缓存保存和显示请检查一次

0

尝试这一个,用于装载图像从符。

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