2013-10-31 83 views
0

我有一個tableview中SDWebImage的UIImageView +中的WebCache定製的UIImageView

此代碼的工作完美,但圖像放置怪異和醜陋的我的tableview從我的web服務器加載圖像的問題到我的UIImageView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Get the cell. Note that this name is the same as in the storyboard 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //Set the correct name in the cell. 
    //Do so by looking up the row in indexpath and choosing the same element in the array 
    NSInteger currentRow = indexPath.row; 
    Image* currentImage = [self.images objectAtIndex:currentRow]; 

    // Here we use the new provided setImageWithURL: method to load the web image 
    [cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]]; 
    return cell; 
} 

所以我創建了一個的UIImageView我的故事板,並改變了代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Get the cell. Note that this name is the same as in the storyboard 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //Set the correct name in the cell. 
    //Do so by looking up the row in indexpath and choosing the same element in the array 
    NSInteger currentRow = indexPath.row; 
    Image* currentImage = [self.images objectAtIndex:currentRow]; 

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100]; 
    // Here we use the new provided setImageWithURL: method to load the web image 
    [imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]]; 
    return cell; 
} 

但是,如果我跑我的設備上運行此代碼,我得到一個西尼亞l SIGABRT錯誤。

我如何才能在我的自定義Imageview上工作?

+0

你確定這個單元有一個viewWithTag 100嗎? – manujmv

+0

是的。現在我已經將它從100改爲1000,並且它正在工作。 –

回答

0

在第一代碼片段,添加這些行:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill; 
cell.imageView.clipsToBounds = YES; 
0

你得到SIGABRT因爲你imagevIview沒有添加到細胞,但細胞的contentView ..!所以正確的方式來獲取imageView應該是,

UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];