2013-03-30 57 views
1

我有一個與UIImageView自定義單元格,它不顯示圖像。我已經嘗試將圖像設置爲默認的cell.imageView.image屬性,它工作得很好,但不適用於我的自定義ImageView。與UIImageView自定義UITableViewCell不顯示圖像

我從Xib加載我的自定義單元格,並且我認爲它與延遲加載UIImageView有關。我如何使它工作? 這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MyCell"; 
    DVGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 



    cell.tag = indexPath.row; 

    if (self.loader.parsedData[indexPath.row] != nil) 
    { 
     cell.imageCustom.image = nil; 

     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
      dispatch_async(queue, ^(void) { 

       NSString *url = [self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]; 
       NSData *imageData = nil; 
       if ([self.cache objectForKey:url] != nil) 
       { 
        imageData = [self.cache objectForKey:url]; 
       } 

       else 
       { 
        imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
       [self.cache setObject:imageData forKey:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]; 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 

        if (cell.tag == indexPath.row) { 
         UIImage *image = [[UIImage alloc] initWithData:imageData]; 
         cell.imageCustom.image = image; 
         [cell setNeedsLayout]; 
        } 
       }); 
      }); 
    } 

    return cell; 
} 
+0

如果在發佈有關同一代碼塊的問題之間只有2小時的差距,那麼您並未努力嘗試自己解決問題。 –

回答

3

我通常做延遲加載與這段代碼ImageViews,希望它有助於:

- (void) loadImageForImageView:(UIImageView *)theImageView WithURL:(NSURL *)url {  

    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:3.0]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *reponse, NSData *data, NSError *error) { 

     UIImage *image = [UIImage imageWithData:data]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      theImageView.image = image; 
      for (UIActivityIndicatorView *spinner in theImageView.subviews) { 
       [spinner removeFromSuperview]; 
       break; 
      } 
     }); 

    }]; 
} 

在你的cellForRowAtIndexPath:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[spinner setColor:[UIColor darkGrayColor]]; 
spinner.frame = CGRectMake(130 , 53, 20, 20); 
[spinner startAnimating]; 
[imageCell addSubview:spinner]; 
[self loadImageForImageView:imageCell WithURL:imageURL]; 

哪裏的ImageCell是你的UIImageView。

相關問題