【发布时间】: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应该从FeedJSON 响应中获取高度。我对此较新,所以我不确定这是否回答了您的问题,我没有做对,但我不知道它是什么。 -
我确定我需要在 106 中添加一个数字,那么有没有办法可以将
imageHeight转换为数字?还是我想错了? -
如果您需要,我已用更多信息更新了问题-
标签: ios objective-c uitableview heightforrowatindexpath