UIImageView加倍并更改大小

时间:2015-06-14 10:21:11

标签: ios objective-c xcode uitableview uiimageview

我有一个问题让我疯了一段时间,我无法解释出现了什么问题。我有一个具有不同视图的UITableView和3个原型单元可供选择。如果我在我的群组模式中,则会在我的故事板中显示原型 applicationCell 。第一个条目总是看起来没问题,但如果我有2个第二个条目中的图像加倍并调整图像大小以便显示两次 - 大小正确并再次调整为单元格高度(每个单元格中也只有一个图像),请参阅图像:

enter image description here

此处导致问题的相关代码:

- (UITableViewCell *)tableView:(UITableView *)table
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(app == nil && groupedMode) {
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"applicationCell"
                                                        forIndexPath:indexPath];
    UILabel *head = (UILabel*)[cell viewWithTag:2];
    UILabel *badge = (UILabel*)[cell viewWithTag:3];
    UILabel *lastMsg = (UILabel*)[cell viewWithTag:4];
    UIImageView *imgView = (UIImageView*)[cell viewWithTag:1];
    ApplicationEntity *a = [applications objectAtIndex:indexPath.row];
    head.text = a.name;
    badge.text = [NSString stringWithFormat:@"%lu",(unsigned long)a.messages.count];
    ImagesEntity *ie = [ImagesEntity imageByAppId:a.appToken imgId:0];
    NSLog(@"V %li",(long)indexPath.row);
    //imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.image = ie.computedImage;
    CGRect r = imgView.bounds;
    NSLog(@"bounds %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
    r = imgView.frame;
    NSLog(@"frame %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
    imgView.bounds = CGRectMake(0,0,48,48);
    imgView.frame = CGRectMake(11,2,48,48);
    cell.tag = indexPath.row;
    if(cell.gestureRecognizers.count == 0) {
        UITapGestureRecognizer * gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(applicationClicked:)];
        [cell addGestureRecognizer:gr];
    }
    lastMsg.text = [dateFormatter stringFromDate:a.lastMessage];
    r = imgView.bounds;
    NSLog(@"after bounds %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
    r = imgView.frame;
    NSLog(@"after frame %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
    return cell;
}

UIImageView对width = height = 48有一个约束。到目前为止我测试并发现了什么:

  1. 将图像设置为nil显示无图像=无错误
  2. 未分配新图像不会显示错误。
  3. 只能分配一个新图像。
  4. 当它发生时,UIImageView获取新的边界和框架,但将它们直接设置回正确的值无助于查看日志

    V 0 边界0.000000 0.000000 48.000000 48.000000 框架11.000000 2.000000 48.000000 48.000000 在边界0.000000 0.000000 48.000000 48.000000之后 框架后11.000000 2.000000 48.000000 48.000000 V 1 边界0.000000 0.000000 320.000000 110.000000 框架0.000000 110.000000 320.000000 110.000000 在边界0.000000 0.000000 48.000000 48.000000之后 框架后11.000000 2.000000 48.000000 48.000000

  5. 110高是单元高度,320是宽度。它违反了约束条件。此外,我指定的图像为96x96像素,因此在HDPI显示中显示效果很好。

    排序和图像可能会改变,所以我需要分配正确的图像。

    为什么会发生这种情况以及如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

经过一些谷歌搜索相关的答案后,我发现至少有一个解决方案和想法发生了什么。

表格单元格已经拥有自己的图像。我的代码

UIImageView *imgView = (UIImageView*)[cell viewWithTag:1];

应该返回我的UIImageView,因为它是唯一一个带有标签1的设置。显然它不是。在故事板中将标记更改为10并且代码解决了它。所以我正在以某种方式改变细胞的背景图像。

奇怪的是,另一种模式也有原型单元格,图像为标记1,它们没有这种问题。所以我仍然不知道它为什么会发生,但至少我知道如何修复它。希望这对有类似问题的其他人有用。