OGeek|极客世界-中国程序员成长平台

标题: ios - UIImageView 翻倍并改变大小 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:59
标题: ios - UIImageView 翻倍并改变大小

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

enter image description here

这里是导致问题的相关代码:

- (UITableViewCell *)tableViewUITableView *)table
     cellForRowAtIndexPathNSIndexPath *)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 actionselector(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. 将图像设置为零显示无图像 = 无错误
  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

110 高度是单元格高度,320 是宽度。它违反了约束。此外,我分配的图像是 96x96 像素,因此在 HDPI 显示器中显示效果很好。

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

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



Best Answer-推荐答案


通过谷歌搜索相关答案后,我至少找到了一个解决方案并知道发生了什么。

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

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

应该返回我的 UIImageView,因为它是唯一一个带有标签 1 的集合。显然它不是。在 Storyboard 中将标签更改为 10,代码解决了它。所以我以某种方式改变了单元格的背景图像。

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

关于ios - UIImageView 翻倍并改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30828232/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4