Adrian P
Adrian P

Reputation: 6529

setting the size of the image added to cell in tableview

I am adding images to my cell view from a plist. Also I have the same issue with images that I save to the plist using a url as well. They are different sizes. I'd like to resize them to certain size so they would all appear the same. I have tried to use:

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

It did not work. I have tried a bunch of other things and did not succeed. I have looked up a bunch of stuff and can't find anything helpful. Here is my cell for row method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
        cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES;
    } else {
        cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
        // Construct the expected URL for the image.
        NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
        imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
        // Attempt to load the image at the above URL.
        cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
        if (!cell.imageView.image)
            cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES;
    }

    return cell;
}

For the life of me I can't figure this out. I know it might be a easy fix but i can't think of anything else.

Edit:

As suggested, I created a custom cell, declared layoutSubviews in there and then subclassed the cell as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    MyCostumCell *cell = (MyCostumCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyCostumCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
        cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
        //cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:1];
    } else {

        cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
        cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
        // Construct the expected URL for the image.
        NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
        imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
        // Attempt to load the image at the above URL.
        cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
        if (!cell.imageView.image)
            cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
    }

    return cell;
}

Again, it did not work. Here is the screen shot:

enter image description here

Upvotes: 1

Views: 11361

Answers (2)

vikingosegundo
vikingosegundo

Reputation: 52237

you would subclass UITableViewCell. but you dont have to create a whole new view hierachy as proposed by Dante, but overwrite -layoutSubviews

@interface VideoItemCell : UITableViewCell
@end


@implementation VideoItemCell
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100);
}

@end

In the tableview controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    VideoItemCell *cell = (VideoItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[VideoItemCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    }

    //…
    return cell;
}

Upvotes: 7

Shubhank
Shubhank

Reputation: 21805

Default TableViewCell resizes imageview based on image size. To solve this you can use a custom cell having a custom Image View ..

More info on how to create custom cell

How do you load custom UITableViewCells from Xib files?

Upvotes: 1

Related Questions