14

I am loading in a list of facebook users using webcache and it works fantastically. Until you select one of the cells then it seems to either change the content mode, or more likely it changes the size of the uiimageview frame, but based on the actual size of the picture. for clarity here are some screens

here it is loaded http://img.photobucket.com/albums/v246/homojedi/Screenshot20120727114807.png

and on selection of some of the images as you can see they seem to jump to their original aspect. http://img.photobucket.com/albums/v246/homojedi/Screenshot20120727114827.png

as expected if i scroll them off screen and back to them they restore to what they were at the start.

It's baffling. The only thing i have not attempted is subclassing the uitableView and setting its layout subview there. short of that is there anything else i can do?

EDIT: code requested

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // typically you need know which item the user has selected.
    // this method allows you to keep track of the selection
    _indexPath = indexPath;

    [_indexPath retain];
}
Genhain
  • 1,937
  • 1
  • 19
  • 38
  • Can you post your code for `tableView:didSelectCellAtIndexPath:`? – jonkroll Jul 27 '12 at 04:11
  • I have not implemented That method, Does it exist? i do have this - (void)tableView:tableView didSelectRowAtIndexPath and it is essentiall empty all i do is retain the indexpath for possible later use. – Genhain Jul 27 '12 at 04:32
  • Did You worked it out? I'm dealing with same problem right now, any help will be gladly aprreciated! – Sebastian Łuczak Aug 07 '12 at 19:59
  • I'm having the same issue but I'm using the standard tableView (i.e. no custom views), tried all the above solutions and they don't work for me. Any idea why? – zaam Mar 21 '19 at 02:18

10 Answers10

58

I've face exactly the same issue as you,and I fix

the issue is cause by you named your imageView "imageView"

i don't know why

if you change your imageView name like "m_imageView"

the issue fixed

It's all about naming!

Bird Hsuie
  • 690
  • 7
  • 15
  • 2
    Looks like UITableViewCell already has an internal property `imageView` as well, which would explain why behavior gets weird. – John K. Chow Feb 03 '14 at 06:28
  • 1
    It was the naming for me, too. Confused me for a few hours, and even when I set a new name, I forgot to unlink the old one, so the bug still occurred. So don't do that! – Peter Johnson May 11 '14 at 00:16
  • I don't understand this solution. If you use a UITableViewCell, it has an imageView and you do not need to name it. Are you guys adding your own, additional image view to the cells? – SwiftMatt Jan 17 '16 at 07:44
  • my god. I spent half a day thinking this was a constraints issue that was causing this ridiculous layout. I was warned about naming the `UIImageView`, `imageView`, but I didn't delete the referenced outlet. – Alan May 02 '16 at 15:20
  • In my case the UILAbel was moving just beacuse I left in XIB reference to "textLabel", after debugger. told me that I cannot overwrite textLabel of UITableViewCell. The cell probably recognised populated with UILabel "textLabel" and was trying to redraw it. – Paweł Brewczynski Apr 21 '17 at 06:56
19

I have come across this problem but I have no imageView property inside my custom UITableViewCell.

In fact I did make a stupid mistake by connecting an extra IBOutlet from my imageView object to the default imageView property of UITableViewCell. It is because I initially named my image view imageView, but then I changed it and forget about the outlet.

extra outlet

After removing the outlet, everything works fine.

Jakehao
  • 1,019
  • 8
  • 15
7

Just in case someone else, like me, come upon this issue where non of the above worked.

I too had named my UIImageView: imageView. But, for some reason, even after renaming it, the problem persisted. I tried everything I could think of, Reset Simulator, Clean Project, adding removing constrains....

As it turns out, simply deleting the UIImageView from the Storyboard and dragging a new one fixed it.

Couldn't have been a precompiled issue because I cleaned the project. Go figure.

bauerMusic
  • 5,470
  • 5
  • 38
  • 53
  • The reason this works is most likely the same as Jakehao's answer: You still had an old IBOutlet to imageView dongling in there. Well, deleting the view and replacing it is one way to get rid of that. Nevertheless it really helped me, so have my upvote, sir. :) – RyuX51 Jul 23 '17 at 16:14
2

I was having this issue and I solved it!!!

Check to make sure you synthesized the imageView property in your custom TableViewCell.

I forgot to synthesize a property (resulting in no getter/setter for the IBOutlet).

This topic is old but hope that helps someone else!

Nadeem
  • 128
  • 1
  • 6
  • This solution worked for me, is there any reason why we have to synthesize the property manually. As of xcode 4.4 synthesize should be optional. – Dennis Jan 31 '14 at 12:37
2
// reason for this problem is UITableView cell's imageview initially has a default size later on it resize it self on select so following cab be a solution for this: (Using AFNetworking to set image/ You can adjust accordingly)

            __weak UItableViewCell *cellTemp = cell;

            [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]
                                  placeholderImage:[UIImage imageNamed:@"placeholder"]
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
            {
                cellTemp.imageView.image = image;

                CGRect frameRect = cellTemp.imageView.frame;
                frameRect.origin.y = (cellTemp.frame.size.height - image.size.height)/2;
                frameRect.size.width = image.size.width;
                frameRect.size.height = image.size.height;
                cellTemp.imageView.frame = frameRect;

                [cellTemp layoutSubviews]; 
    // this will fit your cell's label

            }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
            {

            }];
Venu Gopal Tewari
  • 5,672
  • 42
  • 41
1

I believe your problem is due to the UIImageView. You should be setting the image once - cell.imageView.image = myImage. Do not set a highlightedImage. Set the contentMode to maintain the aspect ratio of your image: cell.imageView.contentMode = UIViewContentModeScaleAspectFit;, and verify that the contentStretch rect is 0,0,1,1 (the default).

It is possible the tableView is changing the contentMode of the UIImageView or its frame when it gets selected (for who knows why reasons). I would add NSLogs to both willSelectRowAtIndexPath and didSelectRowAtIndexPath, showing the same information: the imageView frame, the contentMode value (as an integer), etc. Somehow one or more of these values is changing on selection.

David H
  • 40,852
  • 12
  • 92
  • 138
1

Adding to the answers regarding the naming issue:

As it has been said, the problem with the image view persists even after renaming it. But it is not required to delete the UIImageView from your xib or storyboard. You can simply ctrl-click the image view and remove the connection to property imageView that still exists.

The reason the link to imageView still exists seems to be that the imageView property still remains available after you delete it in your code. It's still there because it is inherited from UITableViewCell.

One last hint that sounds stupid but took me a few minutes to realize: Obviously all code using property name imageView will still work but will result in strange behavior! So double check if there's no code left using the old property name.

olik79
  • 1,402
  • 1
  • 13
  • 16
1

I had the same issue, and even after trying all the previous answers none of them worked out. Later i found out that the UITableViewController had multiple unused Outlets, so deleting the unused Outlets fixed the problem.

Diogo Antunes
  • 2,241
  • 1
  • 22
  • 37
0

As requested by sebastian-Luczak I did come up a solution for resizing, but now there is an image quality inconsistency, it gets higher resolution only when you touch it, strange but not as bad or noticable, anyway here is the code.

N.B i Am using SDWebImage to load the pictures asynchronously

[cell.imageView setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"Icon.png"]success:^(UIImage *image) 
 {
     cell.imageView.image = [image resizeImage:image newSize:CGSizeMake(38, 38)];

 } failure:^(NSError *error) 
 {

 }];
Genhain
  • 1,937
  • 1
  • 19
  • 38
0

// If you subclass UITableViewCell you can get rid of this problem

- (void)layoutSubviews {

    [super layoutSubviews];
    self.imageView.bounds = CGRectMake(10,5,65,65);
    self.imageView.frame = CGRectMake(10,5,65,65);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

}
Chamira Fernando
  • 6,836
  • 3
  • 23
  • 23