programing

그룹화된 스타일로 UITableView에서 셀 너비를 설정하는 방법

muds 2023. 6. 25. 20:38
반응형

그룹화된 스타일로 UITableView에서 셀 너비를 설정하는 방법

저는 이 일을 이틀 정도 하고 있어서, 저는 제가 배운 것을 여러분과 공유한다고 생각했습니다.

질문은 다음과 같습니다.그룹화된 UITableView에서 셀의 너비를 더 작게 만들 수 있습니까?

답은 "아니오"입니다.

하지만 이 문제를 해결할 수 있는 두 가지 방법이 있습니다.

솔루션 #1: 얇은 테이블 테이블 보기의 프레임을 변경하여 테이블이 더 작아지도록 할 수 있습니다.이렇게 하면 UITableView가 축소된 너비로 셀을 안쪽으로 렌더링합니다.

이에 대한 솔루션은 다음과 같습니다.

-(void)viewWillAppear:(BOOL)animated
{
    CGFloat tableBorderLeft = 20;
    CGFloat tableBorderRight = 20;

    CGRect tableRect = self.view.frame;
    tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
    tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
    tableView.frame = tableRect;
}

솔루션 #2: 이미지에 의해 렌더링되는 셀이 있습니다.

이 솔루션에 대한 설명은 다음과 같습니다.

이 정보가 당신에게 도움이 되기를 바랍니다.저는 많은 가능성을 시도하는데 이틀 정도 걸렸습니다.이것이 남아있는 것입니다.

더 하고 UITableViewCell을 하는 것입니다.-setFrame:다음과 같은 방법:

- (void)setFrame:(CGRect)frame {
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}

왜 더 나은가요?왜냐하면 나머지 두 개는 더 나쁘기 때문입니다.

  1. 에서 테이블 뷰 폭-viewWillAppear:

    할 수 없는 또는 뷰 뷰할 수 점입니다. 수퍼뷰 또는 상위 뷰 컨트롤러는 테이블 뷰 프레임을 더 조정할 수 있습니다.-viewWillAppear:이 호출됩니다. 오버라이드를 할 수도 있습니다.-setFrame:여기서 UITableViewCells에 대해 수행하는 작업과 마찬가지로 UITableView에 대해 설명합니다.그러나 UITableViewCells를 하위 분류하는 것은 훨씬 일반적이고 가볍고 Apple 방식입니다.

    두 번째로, UITableView에 backgroundView가 있는 경우 배경 보기가 함께 좁혀지지 않도록 해야 합니다.UITableView 너비를 좁히면서 backgroundView 너비를 유지하는 것은 사소한 일이 아니며, Subview를 Superview 이상으로 확장하는 것은 애초에 매우 우아한 일이 아닙니다.

  2. 좁은 너비를 가장하는 사용자 지정 셀 렌더링

    이를 위해서는 수평 여백이 있는 특수 배경 이미지를 준비해야 하며 여백에 맞게 셀의 하위 뷰를 직접 레이아웃해야 합니다.이에 비해 전체 셀의 너비를 조정하기만 하면 자동 크기 조정이 모든 작업을 수행합니다.

않는 수변를설는에제방지공않하서는다합이작니업재야해대정의를설정자한다음에행수면하을려정하법을ter▁▁for▁the▁to다▁you,▁to합▁override니에 대한 설정자를 재정의해야 .frame원래 여기에 게시됨(적어도 찾은 위치)

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        let inset: CGFloat = 15
        var frame = newFrame
        frame.origin.x += inset
        frame.size.width -= 2 * inset
        super.frame = frame
    }
}

아무 것도 안 되면 이것을 시도할 수 있습니다.

셀의 배경색을 선명한 색으로 만든 다음 필요한 크기의 셀 이미지를 넣습니다.해당 셀에 텍스트를 표시하려면 이미지 위에 레이블을 붙입니다.레이블의 배경색도 선명한 색으로 설정하는 것을 잊지 마십시오.

저는 승인된 해결책이 회전할 때 작동하지 않는다는 것을 발견했습니다.고정된 너비와 유연한 마진으로 UITableViewCells를 달성하기 위해 위의 솔루션을 다음과 같이 적용했습니다.

- (void)setFrame:(CGRect)frame {

    if (self.superview) {
        float cellWidth = 500.0;
        frame.origin.x = (self.superview.frame.size.width - cellWidth) / 2;
        frame.size.width = cellWidth;
    }

    [super setFrame:frame];
}

장치가 회전할 때마다 메소드가 호출되므로 셀은 항상 중앙에 배치됩니다.

화면이 회전할 때 호출되는 메서드가 있습니다. viewWillTransitionToSize

프레임의 크기를 조정해야 합니다.예를 참조하십시오.필요에 따라 프레임 좌표를 변경합니다.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        int x = 0;
        int y = 0;
        self.tableView.frame = CGRectMake(x, y, 320, self.tableView.frame.size.height);
    }];
}

나는 그것을 합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    CGFloat tableBorderLeft = self.view.frame.origin.x + 10;
    CGFloat tableBorderRight = self.view.frame.size.width - 20;

    CGRect tableRect = self.view.frame;
    tableRect.origin.x = tableBorderLeft; 
    tableRect.size.width = tableBorderRight; 
    tableView.frame = tableRect;
}

그리고 이것은 나에게 효과가 있었습니다.

.h 파일에 대리자 'UITableViewDataSource'를 추가합니다.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    return size;
}

언급URL : https://stackoverflow.com/questions/2539021/how-to-set-the-width-of-a-cell-in-a-uitableview-in-grouped-style

반응형