Reputation: 31
I'm new to iOS
and hope someone can give me some guidance.. I have a custom tableviewcell which has a UIImage within it. When the UITableView
loads and cells are created the image loaded remains within the frame of the UIImageView control. When a row selection takes place the image is resized so to fill the height allowed by the cell. Only the row selected is affected at this time. A similar issue arises on return from a child view (which is loaded as a result of pressing a button on the cell).
I've tried placing constraints on the height & width (as well the x, y position within the superview) of the UIImageView, and tried setting the bounds and frame of the UIImageView within the cellForRowAtIndexPath event. None of which seem to have an effect.
Removing all of my attempts at fixing this and getting back to the simplest of code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *favoriteImageFileName = [[NSString alloc] initWithFormat:@"%@/Folder-Favorite_24px.png", documentDirectory];
NSString *videoPlayImageFileName = [[NSString alloc] initWithFormat:@"%@/Video-4_24px.png", documentDirectory];
NSString *imageInQuestion = [[NSString alloc] initWithFormat:@"%@/T-BoneDish128.png", documentDirectory];
SearchResultsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultsTableViewCell"];
UIImage * favoriteIcon = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfFile:favoriteImageFileName]];
UIImage * videoPlay = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfFile:videoPlayImageFileName]];
UIImage * image = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfFile:imageInQuestion]];
[cell.videoPlayButton setBackgroundImage:videoPlay forState:UIControlStateNormal];
[cell.favoriteImage setImage:favoriteIcon];
[cell.imageView setImage:image];
return cell;
}
I'd appreciate any help offered.
Upvotes: 2
Views: 153
Reputation: 31
The UITableCell was subclassed to provide access to the custom attributes (image, labels and button). The names of the properties of the new class matched those of the standard template (i.e. image).
However when populating these properties in code only those in the super where being updated. The subclass properties which were the one actually linked to the outlets of labels, and image on the custom cell remained nil.
So when the image was being displayed it was accessing the image in the super and not those subclass and any constraints did not appear to be applied because the image the constraints were applied to was nil and not being displayed. An important point to remember in future is that whenever you create a custom UITableCell never use any of the names of the properties of the super.
As soon as the properties in the subclass were made different to those of the super, those were updated correctly, the image outlet linked to subclass then had an image to display and the constraints got applied as expected.
You don't get a compiler error or warning when you 'override' properties of a super which could be done inadvertently as was in this case. Hope this helps other IOS/Objective C newbies.
Upvotes: 1