0

次の画面のアプリを作成しています。しかし、思い通りに画像を並べることができません。私が望んでいるのは、画像が歪んだり、テキストを覆ったり、セルの外に漏れたりしないように、できるだけ大きくすることです。画像を縮小するための正しいコードがあると思います。しかし、どの自動サイズ変更マスクを付けるのかわかりません。

ここに画像の説明を入力してください

関連するコードは次のとおりです。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Prototype Cell"];
    }
    Restaurant *restaurantForIndex = [self.restaurants objectAtIndex:indexPath.row];
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor greenColor];
    label.text = restaurantForIndex.name;
    label.font = [UIFont fontWithName:CELL_FONT_NAME size:CELL_FONT_SIZE];
    [cell.contentView addSubview:label];

    cell.backgroundColor = [UIColor whiteColor];

    UIImage *image = [restaurantForIndex getLogo];
    if(image.size.height > CELL_HEIGHT - 2 *CELL_Y_BUFFER)
    {
        float scale = (CELL_HEIGHT - 2 * CELL_Y_BUFFER) / image.size.height;
        image = [UIImage scaleImage:image toSize:CGSizeMake(image.size.width * scale, CELL_HEIGHT - 2 * CELL_Y_BUFFER)];
    }
    float textWidth = [label.text sizeWithFont:[UIFont fontWithName:CELL_FONT_NAME size:CELL_FONT_SIZE]].width;
    label.frame = CGRectMake(CELL_X_BUFFER, CELL_Y_BUFFER, textWidth, cell.contentView.frame.size.height - 2 * CELL_Y_BUFFER);
    label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    if(label.frame.origin.x + textWidth + NAME_LOGO_BUFFER + image.size.width + CELL_X_BUFFER > cell.contentView.frame.size.width)
    {
        float newImageWidth = cell.contentView.frame.size.width - (label.frame.origin.x + textWidth + NAME_LOGO_BUFFER + CELL_X_BUFFER);
        float scale = newImageWidth / image.size.width;
        image = [UIImage scaleImage:image toSize:CGSizeMake(newImageWidth, image.size.height * scale)];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    float xOrigin = cell.contentView.frame.size.width - image.size.width - CELL_X_BUFFER;
    float yOrigin = (cell.contentView.frame.size.height - image.size.height) / 2 ;
    float width = image.size.width;
    float height = image.size.height;
    imageView.frame = CGRectMake(xOrigin, yOrigin, width, height);

    //IMPORTANT LINE OF CODE HERE
    imageView.autoresizingMask =  UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin  | UIViewAutoresizingFlexibleLeftMargin;
    [cell.contentView addSubview:imageView];
    return cell;
}

コードに関するいくつかの注意事項:大文字のBUFFER単語が表示される場合は常に定数であるため、サブビューはセルの端に触れません。

4

0 に答える 0