2013-01-03 25 views
0

我有以下方式加載表視圖。如果符合條件,我需要在cell.imageview上方添加特定圖像。此外,圖像來自不同的維度。下面是我的代碼,任何人都可以指出我錯在哪裏。我的圖像視圖在表格視圖中行爲不正確?

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 
if(array==nil||[array count]==0) 
{ 

} 
else 
{ 
    NSMutableDictionary *dicttable=[array objectAtIndex:indexPath.row]; 
    NSString *head=[dicttable objectForKey:@"name"]; 
    NSString *type=[dicttable objectForKey:@"type"]; 

    NSString *imgUrl = [dicttable objectForKey:@"image"];; 
    if(imgUrl!=nil) 
    { 
     if(![[ImageCache sharedImageCache] hasImageWithKey:imgUrl]) 
     { 
      cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"]; 
      NSArray *myArray = [NSArray arrayWithObjects:cell.imageView,imgUrl,@"noimage_icon.png",[NSNumber numberWithBool:NO],nil]; 
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
      [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray]; 
      cell.imageView.frame=CGRectMake(0,0,48,48); 
      cell.imageView.bounds=CGRectMake(0,0,48,48); 
      [cell.imageView setClipsToBounds:NO]; 
     } 
     else 
     { 
      cell.imageView.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:imgUrl]; 
      cell.imageView.frame=CGRectMake(0,0,48,48); 
      cell.imageView.bounds=CGRectMake(0,0,48,48); 
      [cell.imageView setClipsToBounds:NO]; 
     } 
    } 
    else 
    { 
     cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"]; 
    } 
    if([type isEqualToString:@"YES"]) 
    { 
     UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bluel.png"]]; 
     [cell setBackgroundView:img]; 
     [img release]; 

     cell.textLabel.text = head; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 

     cell.detailTextLabel.textColor=[UIColor grayColor]; 
     cell.detailTextLabel.text = subtitle1; 
     cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
    } 
    else 
    { 
     UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wnew1.png"]]; 
     [cell setBackgroundView:img]; 
     [img release]; 
     cell.textLabel.text = head; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     [cell.imageView addsubview:spclimage] 

     cell.textLabel.text = head; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 

     cell.detailTextLabel.textColor=[UIColor grayColor]; 
     cell.detailTextLabel.text = subtitle1; 
     cell.detailTextLabel.backgroundColor = [UIColor clearColor];    
    } 
} 
return cell; 

這裏問題只出現在特殊圖像的最後一行添加。不是全部行。此外,圖像視圖大小在重新加載tableview的所有時間都不相同。

+0

這就是開玩笑的圖像緩存梅索德其中WE R緩存images.thats all.nothing正在發生的事情更在那裏。 – hacker

+0

@Rob它一個appdelegate方法,我們將圖像存儲到緩存,然後更新圖像。 – hacker

+0

@Rob非常感謝那寶貴的最後評論。如果我需要添加特殊圖像到所有的細胞可以幫助我如何做到這一點? – hacker

回答

1

一對夫婦的想法:

  1. updateImageViewInBackground看來犯罪嫌疑人,顧名思義,你更新的形象圖,但不指定要更新哪個小區。

  2. 我也看到你在做一個addSubview:spclimage。很顯然,如果spclimage在另一個單元格上,那麼只要您做了addSubview,它就會在添加到當前單元格之前從先前的位置移除。實際上,只是將圖像添加爲現有圖像視圖的子視圖的想法很好奇。

  3. 如果你的緩存還沒有圖像,我看到你在用noimage_icon.png初始化圖像,但是我沒有看到你實際更新圖像視圖的位置。你說updateImageViewInBackground是「然後更新圖像」。你的意思是爲設置image屬性爲imageView這個indexPath?或者可能更新spclimage?如果是這樣,這是有問題的。

  4. 典型模式(使用GCD),因爲這將是:

    cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"]; 
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
        UIImage *image = ... // do whatever you need to do to get the image, load cache, etc. 
    
        // ok, now that you have the image, dispatch the update of the UI back to the main queue 
    
        dispatch_async(dispatch_get_main_queue(), ^{ 
    
         // because we're doing this asynchronously, make sure the cell is still 
         // visible (it could have scrolled off and the cell was dequeued and 
         // reused), so we're going to ask the tableview for the cell for that 
         // indexPath, and it returns `nil` if it's not visible. This method is 
         // not to be confused with the similarly named `UITableViewControllerDelegate` 
         // method. 
    
         UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    
         // if the image view is still visible, update it 
    
         if (cell) 
         { 
          cell.imageView.image = image; 
         }  
        }); 
    
    }); 
    
相關問題