2014-09-13 38 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; 
      }); 
     } 
    } 
}); 
相關問題