Question

I'm using an API and need to adjust the UITableViewCell height based on the Image Height.

My if statement needs to be:

  • if there is no image then to set the height to 140, else...
  • if thereis an image then set the height to 106 + imageHeight

I can't seem to get this working, any ideas?

Will post any extra code as needed, thanks!

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
if ([feedLocal.images count] == 0) {
        return 140;
    }
else {
        Images *image = [feedLocal.images objectAtIndex:0];
        NSString *imageHeight = [NSString stringWithFormat:@"%@", image.height];
        return 106 + imageHeight;
    }

I'm getting an error on return 106 + imageHeight; that says "Arithmetic on pointer to interface 'NSString' which is not a constant size for this architecture and platform", and the app crashes when it hits that spot.

EDIT: Content of array (JSON from API)

"feed": [{
        "headline": "xxx",
        "lastModified": "xxxx",
        "images": [{
            "height": 300,
            "alt": "xxx",
            "width": 300,
            "name": "xxx",
            "caption": "xxx",
            "url": "http://xxx.jpg"
        }],

Images.h

@property (nonatomic, strong) NSNumber *height;
@property (nonatomic, strong) NSNumber *width;
@property (nonatomic, strong) NSString *caption;
@property (nonatomic, strong) NSString *url;

+ (RKObjectMapping *) mapping;

Images.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
        @"height", @"height",
         @"width", @"width",
         @"caption", @"caption",
         @"url", @"url",
         nil];
    }];

    return objectMapping;
}
Was it helpful?

Solution

You're attempt to return the addition of a number with a string. Instead use:

    Images *image = [feedLocal.images objectAtIndex:0];
    CGFloat imageHeight = image.size.height;
    return 106 + imageHeight;

OTHER TIPS

have you tried this code?

else {

  Images *image = [feedLocal.images objectAtIndex:0];
  return 106+image.size.height;

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top