1

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

siburb
  • 4,880
  • 1
  • 25
  • 34
Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • you should definitely do some reading of the documentary. your wording is totally confused. In `cellForRowAtIndexPath:` you dont subclass the cell. u use a subclassed cell. – vikingosegundo Oct 08 '13 at 17:40

2 Answers2

7

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;
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • i tried to add the method and i get the error no visible @interface for uitableview declares the selector layoutsubview. – Adrian P Oct 08 '13 at 16:47
  • you need to subclass the cell, not the table view – vikingosegundo Oct 08 '13 at 16:52
  • man, you dont like reading manuals, right? subclassing cells is very much standard. but as you ask super politely: here you go. – vikingosegundo Oct 08 '13 at 16:56
  • thanks for being awesome and try to help. but somehow it did not work for me. i posted the edit part of my question with a screen shot. thanks again for your time. – Adrian P Oct 08 '13 at 17:08
  • it would be helpful if you'd show us your layoutSubviews (please dont tll me, that you didnt adapt it to your needs) – vikingosegundo Oct 08 '13 at 17:38
  • The layout sub view is exactly what you posted. I just created a class for it and ace them there the imposrt the classic tableview and subclasses it as you mentioned. – Adrian P Oct 08 '13 at 17:41
  • yeah, and how am I supposed to know what you need inside? I am not a mind reader. You will ave to adapted it. – vikingosegundo Oct 08 '13 at 17:42
  • iE: I took this code from a app where the row height is at leas 200px. So 100x100px images will fit easily. you have the standart heitht of 44px. – vikingosegundo Oct 08 '13 at 17:48
  • or is it 70px? so try `self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 70, 52); ` – vikingosegundo Oct 08 '13 at 18:06
  • Thank you so much for your help, I'll try to fix the mistakes I possibly made tonight when I get home and let you know. Once again thank you so much for your time. – Adrian P Oct 08 '13 at 18:12
  • Thanks for your input and guidance. Sub classing was the way to go. Although for that app that I needed help with, I went with dumb way of resizing my images but i have adapted the sub classing the cell in the updates and the other projects I'm working on. Thanks again man. – Adrian P Dec 28 '13 at 06:12
1

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?

Community
  • 1
  • 1
Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • thanks for the link, but it is way too old and does not work. also i have tried the custom cell creating the image through a image view and assigned tags to it. that did not work either. anything else you can suggest? – Adrian P Oct 08 '13 at 16:36
  • the answer still works.. you might try to google a proper tutorial step by step which can help you do it properly – Shubhank Oct 08 '13 at 16:56