0

I'm having a problem with loading images from my webserver into my UIImageView in a tableview

This code works perfect, but the images are placed weird and ugly in my tableview:

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

So I created an UIImageView on my storyboard, and changed the code to this:

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

But if I run this code on my device, I'm getting a signal SIGABRT error.

How can I get this working on my custom Imageview?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

In the first code snippet, add these lines:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
0

You are getting SIGABRT because your imagevIview is NOT added to cell, but cell's contentView..! So the right way to get the imageView should be,

UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];
akshaynhegde
  • 1,938
  • 3
  • 17
  • 36