【问题标题】:Cell Row Height based on Image size基于图像大小的单元格行高
【发布时间】:2013-11-13 16:05:16
【问题描述】:

我正在使用 API,需要根据图像高度调整 UITableViewCell 高度。

我的if 声明需要是:

  • 如果没有图片则设置高度为140,否则...
  • 如果有图片,则将高度设置为 106 + imageHeight

我似乎无法让这个工作,有什么想法吗?

将根据需要发布任何额外的代码,谢谢!

- (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;
    }

我在return 106 + imageHeight; 上收到一条错误消息,上面写着“指向接口 'NSString' 的指针的算术运算,这对于该架构和平台来说不是一个恒定的大小”,并且应用程序在到达该位置时会崩溃。

编辑:数组内容(来自 API 的 JSON)

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

图片.h

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

+ (RKObjectMapping *) mapping;

图片.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;
}

【问题讨论】:

  • 你为什么要给一个数字添加一个字符串?还有什么是image.height? UIImage 没有高度属性。
  • @rdelmar 感谢您的回复! image.height 应该从 Feed JSON 响应中获取高度。我对此较新,所以我不确定这是否回答了您的问题,我没有做对,但我不知道它是什么。
  • 我确定我需要在 106 中添加一个数字,那么有没有办法可以将 imageHeight 转换为数字?还是我想错了?
  • 如果您需要,我已用更多信息更新了问题-

标签: ios objective-c uitableview heightforrowatindexpath


【解决方案1】:

您试图返回一个数字与字符串的相加。而是使用:

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

【讨论】:

  • 感谢您的回答,我实际上也尝试过这样做,但在第二行出现错误“在'图像'的对象类型上找不到属性size。我不太清楚确定这是为什么?
  • 你的图像有属性大小吗? feedLocal.images 包含图像?图片是 UIImage 类的子类吗?
  • feedLocal.images 是一个数组,其中有高度和宽度。这就是你要问的吗?谢谢
  • 你能举一个数组内容的例子吗?
  • 添加了数组和Images.h+.m
【解决方案2】:

你试过这个代码吗?

else {

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

 }

【讨论】:

  • 感谢您的回复!我实际上已经尝试过了,它给了我一个错误,即在 Images 类型的对象上找不到属性 size
  • 在上面添加了数组和图像类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
相关资源
最近更新 更多