이미지가 더 작아도 UITableViewCell의 ImageView를 고정 크기로 만드는 방법


104

셀의 이미지 뷰에 사용하는 이미지가 많이 있는데 모두 50x50보다 크지 않습니다. 예 : 40x50, 50x32, 20x37 .....

표보기를로드 할 때 이미지 너비가 다양하기 때문에 텍스트가 정렬되지 않습니다. 또한 왼쪽이 아닌 중앙에 작은 이미지를 표시하고 싶습니다.

다음은 내 'cellForRowAtIndexPath'메서드 내부에서 시도하고있는 코드입니다.

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];

보시다시피 몇 가지 시도했지만 작동하지 않습니다.

답변:


152

모든 것을 다시 쓸 필요는 없습니다. 대신 이렇게하는 것이 좋습니다.

이것을 사용자 지정 셀의 .m 파일에 게시하십시오.

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,32,32);
}

이것은 트릭을 멋지게 할 것입니다. :]


28
설정 self.imageView.bounds하면 이미지가 중앙에 위치합니다.
— BLeB

45
하위 클래스를 추가하지 않으면 UITableViewCell?
— 비극성

3
@ 動靜 能量 : UITableViewCell을 서브 클래 싱하는 것이 이것이 작동하도록하는 주요 트릭입니다.
— auco

5
이것은 나를 위해 작동하지 않습니다. 이미지는 여전히 전체 imageView를 가득 채 웁니다.
— joslinm

14
레이블이 잘못 정렬 되었기 때문에 나에게도 작동하지 않습니다.
— nverinaud 2013 년

139

하위 클래스가없는 분들을 위해 UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 [...]

      CGSize itemSize = CGSizeMake(40, 40);
      UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
      CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
      [cell.imageView.image drawInRect:imageRect];
      cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

 [...]
     return cell;
}

위의 코드는 크기를 40x40으로 설정합니다.

스위프트 2

    let itemSize = CGSizeMake(25, 25);
    UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale);
    let imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    cell.imageView?.image!.drawInRect(imageRect)
    cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

또는 @Tommy가 제안한 다른 (테스트되지 않은) 접근 방식을 사용할 수 있습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 [...]

      CGSize itemSize = CGSizeMake(40, 40);
      UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0)          
 [...]
     return cell;
}

Swift 3+

let itemSize = CGSize.init(width: 25, height: 25)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale);
let imageRect = CGRect.init(origin: CGPoint.zero, size: itemSize)
cell?.imageView?.image!.draw(in: imageRect)
cell?.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!;
UIGraphicsEndImageContext();

위 코드는 위의 Swift 3+ 버전입니다.


3
이미지 왜곡은 UIGraphicsBeginImageContextWithOptions (itemSize, NO, UIScreen.mainScreen.scale); 대신 UIGraphicsBeginImageContext (itemSize);
— Kiran Ruth R

1
좋은 대답입니다. BTW, 나는 옵션을 얻지 못했기 UIScreen.mainScreen.scale때문에 방금 UIGraphicsBeginImageContext. 또한 기본 셀에서 imageView의 크기를 조정했습니다.
— denikov 2014-06-12

3
@GermanAttanasioRuiz가 셀을 선택하면 다시 원본 크기로 조정됩니다.
— Bonnie

6
저처럼 혼란스러워하는 모든 사람들을 위해 컨텍스트 시작 전에 이미지를 설정해야합니다. ie cell.imageView.image = [UIImage imageNamed : @ "my_image.png"];
— Guy Lowe

5
이러한 비용이 많이 드는 작업은 cellForRowAtIndexPath
— Krizai

33

내가 한 방법은 다음과 같습니다. 이 기술은 텍스트 및 세부 텍스트 레이블을 왼쪽으로 적절하게 이동합니다.

@interface SizableImageCell : UITableViewCell {}
@end
@implementation SizableImageCell
- (void)layoutSubviews {
    [super layoutSubviews];

    float desiredWidth = 80;
    float w=self.imageView.frame.size.width;
    if (w>desiredWidth) {
        float widthSub = w - desiredWidth;
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height);
        self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height);
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}
@end

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = ...
    cell.detailTextLabel.text = ...
    cell.imageView.image = ...
    return cell;
}

고마워, 크리스. 이것은 완벽하게 작동했습니다. ARC에서 지금 금지하므로 자동 릴리스를 제거하여 업데이트 할 수 있습니다. 그래도 좋은 대답!
— CSawy

1
이것은 오늘날에도 여전히 최상의 솔루션입니다. 감사합니다.
— 레미 Belzanti

요즘에는 스토리 보드에 xib 또는 프로토 타입 셀이있는 사용자 지정 셀을 만들고 표준 셀의 이미지보기와 관련이없는 다른 전체 이미지보기를 만드는 것이 좋습니다. 그러나 이것은 여전히 ​​충분히 간단합니다.
— Chris

1
xib 또는 스토리 보드를 사용하는 대신 코드로 모든 작업을 수행하고 싶습니다.
— John81

이 대답은 w <desiredWith 인 경우 아무 작업도 수행하지 않습니다. 이는 관심있는 유스 케이스 인 것 같습니다 (적어도 질문에서는).
— Nate

21

이미지보기 테이블보기 셀에 하위보기로 추가

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 90, 70)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[cell.contentView addSubview:imgView];

1
ARC를 사용하지 않는 경우 imgView를 릴리스하는 것을 잊지 마십시오.
— Charlie Monroe

14

전체 세포를 다시 만들 필요가 없습니다. tableViewCells의 indentationLevel 및 indentationWidth 속성을 사용하여 셀의 내용을 이동할 수 있습니다. 그런 다음 셀 왼쪽에 사용자 정의 imageView를 추가합니다.


6

더 나은 이미지보기를 만들고 셀에 하위보기로 추가하면 원하는 프레임 크기를 얻을 수 있습니다.


방금 시도했지만 시작은 좋아 보이지만 셀의 텍스트가 이제 이미지와 겹칩니다. 콘텐츠보기를 오른쪽으로 50 픽셀 이동하려면 어떻게해야합니까? cell.contentView.bounds = CGRectMake (50, 0, 270, 50); 효과가 없습니다
— 로버트

1
셀 기본보기를 사용하는 대신 레이블을 만들고 셀에 하위보기로 추가 한 다음 레이블 텍스트 속성에 텍스트를 할당합니다. 이를 통해 요구 사항에 따라 셀을 설계 할 수 있습니다.
— Warrior

제목, 날짜, 설명 등을 셀에 더 많은 값을 표시하려는 경우 더 유용합니다.
— Warrior

좋아, 그래서 기본적으로 프로그램 적으로 세포를 다시 만들어야한다. 너무 어렵지 않아야합니다. 도와 주셔서 감사합니다.
— 로버트

6

간단히 스위프트 ,

1 단계 : UITableViewCell
2 단계의 하위 클래스 하나 만들기 : 이 메서드를 UITableViewCell의 하위 클래스에 추가합니다 .

override func layoutSubviews() {
    super.layoutSubviews()
    self.imageView?.frame = CGRectMake(0, 0, 10, 10)
}

3 단계 : 에서 해당 하위 클래스를 사용하여 셀 개체 만들기 cellForRowAtIndexPath,

Ex: let customCell:CustomCell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

4 단계 : 즐기기


2
UIImage *image = cell.imageView.image;

UIGraphicsBeginImageContext(CGSizeMake(35,35));
// draw scaled image into thumbnail context

[image drawInRect:CGRectMake(5, 5, 35, 35)]; //
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
{
    NSLog(@"could not scale image");
    cell.imageView.image = image;
}
else
{
    cell.imageView.image = newThumbnail;
}

2

이것은 신속하게 나를 위해 일했습니다.

UITableViewCell의 하위 클래스를 만듭니다 (스토리 보드에서 셀을 연결해야 함).

class MyTableCell:UITableViewCell{
    override func layoutSubviews() {
        super.layoutSubviews()

        if(self.imageView?.image != nil){

            let cellFrame = self.frame
            let textLabelFrame = self.textLabel?.frame
            let detailTextLabelFrame = self.detailTextLabel?.frame
            let imageViewFrame = self.imageView?.frame

            self.imageView?.contentMode = .ScaleAspectFill
            self.imageView?.clipsToBounds = true
            self.imageView?.frame = CGRectMake((imageViewFrame?.origin.x)!,(imageViewFrame?.origin.y)! + 1,40,40)
            self.textLabel!.frame = CGRectMake(50 + (imageViewFrame?.origin.x)! , (textLabelFrame?.origin.y)!, cellFrame.width-(70 + (imageViewFrame?.origin.x)!), textLabelFrame!.height)
            self.detailTextLabel!.frame = CGRectMake(50 + (imageViewFrame?.origin.x)!, (detailTextLabelFrame?.origin.y)!, cellFrame.width-(70 + (imageViewFrame?.origin.x)!), detailTextLabelFrame!.height)
        }
    }
}

cellForRowAtIndexPath에서 새 셀 유형으로 셀을 대기열에서 빼십시오.

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyTableCell

레이아웃에 맞게 숫자 값을 분명히 변경하십시오.


1

@GermanAttanasio의 답변을 사용하여 확장 프로그램을 만들었습니다. 이미지의 크기를 원하는 크기로 조정하는 방법과 이미지에 투명한 여백을 추가하는 동안 동일한 작업을 수행하는 또 다른 방법을 제공합니다 (이는 이미지에 여백을 두려는 테이블보기에 유용 할 수 있습니다).

import UIKit

extension UIImage {

    /// Resizes an image to the specified size.
    ///
    /// - Parameters:
    ///     - size: the size we desire to resize the image to.
    ///
    /// - Returns: the resized image.
    ///
    func imageWithSize(size: CGSize) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale);
        let rect = CGRectMake(0.0, 0.0, size.width, size.height);
        drawInRect(rect)

        let resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return resultingImage
    }

    /// Resizes an image to the specified size and adds an extra transparent margin at all sides of
    /// the image.
    ///
    /// - Parameters:
    ///     - size: the size we desire to resize the image to.
    ///     - extraMargin: the extra transparent margin to add to all sides of the image.
    ///
    /// - Returns: the resized image.  The extra margin is added to the input image size.  So that
    ///         the final image's size will be equal to:
    ///         `CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2)`
    ///
    func imageWithSize(size: CGSize, extraMargin: CGFloat) -> UIImage {

        let imageSize = CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2)

        UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.mainScreen().scale);
        let drawingRect = CGRect(x: extraMargin, y: extraMargin, width: size.width, height: size.height)
        drawInRect(drawingRect)

        let resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return resultingImage
    }
}

1

다음은 Swift 3 용으로 작성된 @germanattanasio의 작업 방법입니다.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    cell.imageView?.image = myImage
    let itemSize = CGSize(width:42.0, height:42.0)
    UIGraphicsBeginImageContextWithOptions(itemSize, false, 0.0)
    let imageRect = CGRect(x:0.0, y:0.0, width:itemSize.width, height:itemSize.height)
    cell.imageView?.image!.draw(in:imageRect)
    cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
}

1

사용 cell.imageView?.translatesAutoresizingMaskIntoConstraints = false하는 경우 imageView에 제약 조건을 설정할 수 있습니다. 다음은 프로젝트에서 사용한 작업 예제입니다. 나는 서브 클래 싱을 피하고 프로토 타입 셀로 스토리 보드를 만들 필요가 없었지만 실행하는 데 꽤 오랜 시간이 걸렸으므로 더 간단하거나 더 간결한 방법을 사용할 수없는 경우에만 사용하는 것이 가장 좋습니다.

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
}



    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: String(describing: ChangesRequiringApprovalTableViewController.self))

    let record = records[indexPath.row]

    cell.textLabel?.text = "Title text"

    if let thumb = record["thumbnail"] as? CKAsset, let image = UIImage(contentsOfFile: thumb.fileURL.path) {
        cell.imageView?.contentMode = .scaleAspectFill
        cell.imageView?.image = image
        cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
        cell.imageView?.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor).isActive = true
        cell.imageView?.widthAnchor.constraint(equalToConstant: 80).rowHeight).isActive = true
        cell.imageView?.heightAnchor.constraint(equalToConstant: 80).isActive = true
        if let textLabel = cell.textLabel {
            let margins = cell.contentView.layoutMarginsGuide
            textLabel.translatesAutoresizingMaskIntoConstraints = false
            cell.imageView?.trailingAnchor.constraint(equalTo: textLabel.leadingAnchor, constant: -8).isActive = true
            textLabel.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
            textLabel.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
            let bottomConstraint = textLabel.bottomAnchor.constraint(equalTo: margins.bottomAnchor)
            bottomConstraint.priority = UILayoutPriorityDefaultHigh
            bottomConstraint.isActive = true
            if let description = cell.detailTextLabel {
                description.translatesAutoresizingMaskIntoConstraints = false
                description.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
                description.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
                cell.imageView?.trailingAnchor.constraint(equalTo: description.leadingAnchor, constant: -8).isActive = true
                textLabel.bottomAnchor.constraint(equalTo: description.topAnchor).isActive = true
            }
        }
        cell.imageView?.clipsToBounds = true
    }

    cell.detailTextLabel?.text = "Detail Text"

    return cell
}

0

일반 UITableViewCell은 위치를 지정하는 데 잘 작동하지만 cell.imageView는 원하는대로 작동하지 않는 것 같습니다. 먼저 cell.imageView에 적절한 크기의 이미지를 제공하여 UITableViewCell이 올바르게 배치되도록하는 것이 충분히 간단하다는 것을 알았습니다.

// Putting in a blank image to make sure text always pushed to the side.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(kGroupImageDimension, kGroupImageDimension), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = blank;

그런 다음 제대로 작동하는 UIImageView를

// The cell.imageView increases in size to accomodate the image given it.
// We don't want this behaviour so we just attached a view on top of cell.imageView.
// This gives us the positioning of the cell.imageView without the sizing
// behaviour.
UIImageView *anImageView = nil;
NSArray *subviews = [cell.imageView subviews];
if ([subviews count] == 0)
{
    anImageView = [[UIImageView alloc] init];
    anImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [cell.imageView addSubview:anImageView];

    NSLayoutConstraint *aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:cell.imageView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    [cell.imageView addConstraint:aConstraint];

    aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:cell.imageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
    [cell.imageView addConstraint:aConstraint];

    aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:kGroupImageDimension];
    [cell.imageView addConstraint:aConstraint];

    aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:kGroupImageDimension];
    [cell.imageView addConstraint:aConstraint];
}
else
{
    anImageView = [subviews firstObject];
}

anImageView에 이미지를 설정하면 UIImageView가 예상하는 작업을 수행합니다. 제공하는 이미지에 관계없이 원하는 크기가 되십시오. 이것은 tableView : cellForRowAtIndexPath에 있어야합니다.


0

이 솔루션은 기본적으로 주어진 직사각형 내에서 이미지를 '종횡비 맞춤'으로 그립니다.

CGSize itemSize = CGSizeMake(80, 80);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
UIImage *image = cell.imageView.image;

CGRect imageRect;
if(image.size.height > image.size.width) {
    CGFloat width = itemSize.height * image.size.width / image.size.height;
    imageRect = CGRectMake((itemSize.width - width) / 2, 0, width, itemSize.height);
} else {
    CGFloat height = itemSize.width * image.size.height / image.size.width;
    imageRect = CGRectMake(0, (itemSize.height - height) / 2, itemSize.width, height);
}

[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

0

나는 같은 문제가 있었다. 답변 해 주신 모든 분들께 감사드립니다.이 답변 중 일부를 사용하여 함께 해결책을 찾을 수있었습니다.

내 솔루션은 Swift 5를 사용하고 있습니다.

우리가 해결하려는 문제는 TableViewCells 에 다른 종횡비를 가진 이미지가있을 수 있지만 일정한 너비로 렌더링하기를 원한다는 것입니다. 물론 이미지는 왜곡없이 렌더링되고 전체 공간을 채워야합니다. 제 경우에는 길고 마른 이미지를 "자르기"하는 것이 괜찮 았기 때문에 콘텐츠 모드를 사용했습니다..scaleAspectFill

이를 위해 UITableViewCell. 제 경우에는 이름을StoryTableViewCell . 전체 수업은 주석과 함께 아래에 붙여 넣어집니다.

이 접근 방식은 사용자 지정 액세서리보기 및 긴 텍스트 레이블을 사용할 때도 효과적이었습니다. 다음은 최종 결과 이미지입니다.

일관된 이미지 너비로 렌더링 된 테이블보기

class StoryTableViewCell: UITableViewCell {

    override func layoutSubviews() {
        super.layoutSubviews()

        // ==== Step 1 ====
        // ensure we have an image
        guard let imageView = self.imageView else {return}

        // create a variable for the desired image width
        let desiredWidth:CGFloat = 70;

        // get the width of the image currently rendered in the cell
        let currentImageWidth = imageView.frame.size.width;

        // grab the width of the entire cell's contents, to be used later
        let contentWidth = self.contentView.bounds.width

        // ==== Step 2 ====
        // only update the image's width if the current image width isn't what we want it to be
        if (currentImageWidth != desiredWidth) {
            //calculate the difference in width
            let widthDifference = currentImageWidth - desiredWidth;

            // ==== Step 3 ====
            // Update the image's frame,
            // maintaining it's original x and y values, but with a new width
            self.imageView?.frame = CGRect(imageView.frame.origin.x,
                                           imageView.frame.origin.y,
                                           desiredWidth,
                                           imageView.frame.size.height);

            // ==== Step 4 ====
            // If there is a texst label, we want to move it's x position to
            // ensure it isn't overlapping with the image, and that it has proper spacing with the image
            if let textLabel = self.textLabel
            {
                let originalFrame = self.textLabel?.frame

                // the new X position for the label is just the original position,
                // minus the difference in the image's width
                let newX = textLabel.frame.origin.x - widthDifference
                self.textLabel?.frame = CGRect(newX,
                                               textLabel.frame.origin.y,
                                               contentWidth - newX,
                                               textLabel.frame.size.height);
                print("textLabel info: Original =\(originalFrame!)", "updated=\(self.textLabel!.frame)")
            }

            // ==== Step 4 ====
            // If there is a detail text label, do the same as step 3
            if let detailTextLabel = self.detailTextLabel {
                let originalFrame = self.detailTextLabel?.frame
                let newX = detailTextLabel.frame.origin.x-widthDifference
                self.detailTextLabel?.frame = CGRect(x: newX,
                                                     y: detailTextLabel.frame.origin.y,
                                                     width: contentWidth - newX,
                                                     height: detailTextLabel.frame.size.height);
                print("detailLabel info: Original =\(originalFrame!)", "updated=\(self.detailTextLabel!.frame)")
            }

            // ==== Step 5 ====
            // Set the image's content modoe to scaleAspectFill so it takes up the entire view, but doesn't get distorted
            self.imageView?.contentMode = .scaleAspectFill;
        }
    }
}

0

우리가 얻은 솔루션은 다른 많은 솔루션과 유사합니다. 그러나 구분 기호의 정확한 위치를 얻으려면을 호출하기 전에 설정해야했습니다 super.layoutSubviews(). 단순화 된 예 :

class ImageTableViewCell: UITableViewCell {

    override func layoutSubviews() {
        separatorInset.left = 70
        super.layoutSubviews()

        imageView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        textLabel?.frame = CGRect(x: 70, y: 0, width: 200, height: 50)
    }

}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.