簡體   English   中英

UIImageView 翻倍並改變大小

[英]UIImageView doubles and changes size

我有一個問題讓我發瘋了一段時間,我無法解釋出了什么問題。 我有一個 UITableView 具有不同的視圖和 3 個原型單元格可供選擇。 如果我處於我的組模式,我會從我的故事板中顯示原型applicationCell 第一個條目總是看起來不錯,但是如果我有 2,第二個條目中的圖像會加倍並調整圖像大小,以便顯示兩次 - 以正確的大小並再次調整為單元格高度(每個單元格中也只有一個圖像),請參閱圖像:

在此處輸入圖片說明

這里是導致問題的相關代碼:

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

110 高度是單元格高度,320 是寬度。 它違反了約束。 此外,我分配的圖像是 96x96 像素,因此在 HDPI 顯示器中顯示效果很好。

排序和圖像可能會發生變化,因此我需要分配正確的圖像。

任何想法為什么會發生這種情況以及我如何防止這種情況發生?

在谷歌搜索相關答案后,我至少找到了一個解決方案和一個想法。

表格單元格已經有了自己的圖像。 還有我的代碼

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

應該返回我的 UIImageView 因為它是唯一一個帶有標記 1 的集合。顯然它不是。 在故事板和代碼中將標簽更改為 10 解決了它。 所以我以某種方式改變了單元格的背景圖像。

奇怪的是,另一種模式也有原型單元,圖像為標簽1,它們沒有這種問題。 所以我仍然不知道為什么它真的會發生,但至少我知道如何解決它。 希望這對其他有類似問題的人有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM