programing

UITableView에서 마지막 셀의 마지막 테두리를 제거하는 방법은 무엇입니까?

codeshow 2023. 8. 10. 21:43
반응형

UITableView에서 마지막 셀의 마지막 테두리를 제거하는 방법은 무엇입니까?

앱에서 저는 서앱에, 는나를 합니다.UITableView문제는 마지막 셀의 마지막 테두리를 제거하려는 것입니다.UITableView.

다음 이미지를 확인하십시오.

enter image description here

바닥글 보기를 추가하는 것이 가장 좋습니다.다음 코드는 마지막 셀의 라인을 완벽하게 숨깁니다.

목표-C

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];

스위프트 4.0

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))

iOS 7에는 더 쉬운 솔루션이 있습니다.셀이 마지막 셀이라고 가정합니다.

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);

9/14/15에 업데이트됨.제 원래 대답은 쓸모없게 되었지만, 여전히 모든 iOS 버전에 대한 보편적인 해결책입니다.

숨을 수 있습니다.tableView의 표준 구분선을 표시하고 각 셀의 맨 위에 사용자 정의 선을 추가합니다.사용자 지정 구분 기호를 추가하는 가장 쉬운 방법은 단순을 추가하는 것입니다.UIView 높이 높이:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:separatorLineView];

현재까지 셀 아래에 별도의 구분 기호를 숨기는 다른 방법을 구독하고 있습니다(iOS 6.1+에서 작동).

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

이것은 iOS 7과 8에서 작동합니다.

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));

참고: 사에주십오시하의용을 사용할 때 .tableView.bounds호출 시간에 따라 잘못된 값을 보고하는 경우가 있기 때문입니다.가로 모드에서 잘못된 경계 보고를 참조하십시오.

다에이 코라추가에 이 합니다.viewDidLoad().

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))

이렇게 하면 마지막 구분 기호가 교체되고 (보이지 않는) 바닥글 보기가 추가 높이를 차지하지 않습니다.보기의 는 바글보너기다비음의에 의해 입니다.UITableView0 로 설 수 있 습 니 다 할 정 으 니 다 있 ▁0 ▁it 습 ▁so ▁set 수 ▁to▁can ▁you

iOS 12에서 테스트된 Swift 4용 확장 기반 솔루션

의 빈 보기를 설정하는 것을 알아챘습니다.height = 1의 구분자도 되지만 설정은 "" " " " " " " " 입니다. " " , "height = 0만 하면 됩니다.

extension UITableView {
    func removeSeparatorsOfEmptyCells() {
        tableFooterView = UIView(frame: .zero)
    }

    func removeSeparatorsOfEmptyCellsAndLastCell() {
        tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 1)))
    }
}

나의 짧은 버전:

self.tblView.tableFooterView = [UIView new];

Swift에서 간단히:

tableView.tableFooterView = UIView()

여기 제가 현재 사용하고 있는 약이 있습니다.최선의 방법은 아닐 수도 있지만 효과가 있습니다.

구분 기호 색상을 제거하고 구분 기호 스타일을 설정하여 사용자 지정 구분 기호 만들기

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setSeparatorColor:[UIColor clearColor]];
}

테이블 뷰 배경이 있는 각 셀에 사용자 정의 구분 기호 추가

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    ...

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, 320, 1)];
    separatorLineView.backgroundColor = self.tableView.backgroundColor;
    [cell.contentView addSubview:separatorLineView];

    return cell;
}

iOS 7 이상의 경우

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        BOOL bIsLastRow = NO;

        //Add logic to check last row for your data source
        NSDictionary *dict = [_arrDataSource objectAtIndex:indexPath.section];
        if([_arrDataSource lastObject] == dict)
        {
           bIsLastRow = YES;
        }

        //Set last row separate inset left value large, so it will go outside of view
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) 
        {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, bIsLastRow ? 1000 :0, 0, 0)];
        }
    }

스위프트 4.2

, ▁your▁this▁to▁that▁in다▁add▁separator니,합추가▁a다음을▁your▁cell▁get▁goes마면려▁from져지▁to▁last에 추가하세요.UITableViewDataSource's tableView(_:cellForRowAt:)구현:

if tableView.numberOfRows(inSection: indexPath.section) - 1 == indexPath.row {
        cell.separatorInset = .zero
}

셀에 하지 않다면 특 셀 에 대 한 구 분 기 지 사 를 하 않 면 가 사 다 니 설 합 정 서 그 려 기 를 구 호 직 정 분 접 자 용 용 으 려 호 ▁if 다 ▁your 니 , ▁consider ▁partic 합 ▁set 특 ▁drawing ▁one ▁and ▁you 정 ▁for ▁separator 설 ▁cell ular ▁separator ▁don ▁owntableView.separatorStyle = .none.

_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

아래 코드는 UI 테이블 보기의 마지막 행에서 구분 기호를 제거하는 데 도움이 됩니다.

스위프트 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
        cell.separatorInset.right = cell.bounds.size.width
    }
}

Xcode 12, 스위프트 5

마지막 셀 다음에 구분 기호를 제거하려면 그룹화된 UITableView 스타일을 선택하십시오. 인터페이스 작성기에서 이 작업을 수행하거나 뷰에서 설정DidLoad

tableView.style = UITableView.Style.grouped

첫 번째 셀 앞에 빈 공간을 제거하고 viewDidLoad에 다음 코드를 기록합니다.

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 0.1))

셀 앞의 빈 공간을 제거하고 탐색 모음 아래로 스크롤하거나 빈 공간을 피하는 데 다른 어려움이 있을 경우, 테이블을 컨트롤러의 수퍼 뷰로 제한하십시오. 여기서 상단 제약 조건을 두 번 클릭한 다음 '슈퍼뷰'를 선택하십시오.제약 조건 설정에서 '위'

전체 너비 구분 기호 또는 해당 문자의 너비를 원하는 경우 사용자 지정으로 구분 기호 삽입을 선택하고 값을 구성하거나 viewDidLoad:

tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

이것은 저에게 매우 유용합니다.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? yourCell else {
            return
    }
    // Here we're checking if your cell is the last one
    if indexPath.row == numberOfCells.count - 1 {
        cell.separatorInset.left = UIScreen.main.bounds.width
    }
}

여기서 수행하는 작업은 왼쪽 삽입물의 크기를 충분히 큰 값으로 설정하여 구분선이 더 이상 표시되지 않도록 하는 것입니다.따라서 삽입물의 크기 값을 늘리면 왼쪽 삽입물이 오른쪽 방향으로 이동하기 때문에 화면의 오른쪽에 점점 더 가까워집니다.

에마막 셀 cellForRow코드 라인을 아래에서 위임하고 배치합니다.

cell.separatorInset = UIEdgeInsetsMake(
    0.f,
    40.0f,
    0.f,
    self.view.frame.size.width-40.0f
);

다른 옵션은 하위 분류를 사용하는 것입니다.UITableView:

@synthesize hideLastSeparator = _hideLastSeparator;
@synthesize hideLastSeparatorView = _hideLastSeparatorView;
-(void)setHideLastSeparator:(BOOL)hideLastSeparator {
    if (_hideLastSeparator == hideLastSeparator) {
        return;
    }
    _hideLastSeparator = hideLastSeparator;
    if (_hideLastSeparator) {
        _hideLastSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentSize.height, self.bounds.size.width, 0.5f)];
        _hideLastSeparatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        _hideLastSeparatorView.backgroundColor = self.backgroundColor;
        [self addSubview:_hideLastSeparatorView];
        [self hideSeparator];
    }
    else {
        [_hideLastSeparatorView removeFromSuperview];
        _hideLastSeparatorView = nil;
    }
}
-(void)setContentSize:(CGSize)contentSize {
    [super setContentSize:contentSize];
    if (_hideLastSeparator) {
        [self hideSeparator];
    }
}
-(void)hideSeparator {
    CGRect frame = _hideLastSeparatorView.frame;
    frame.origin.y = self.contentSize.height - frame.size.height;
    _hideLastSeparatorView.frame = frame;
}

는 .h에 대한 .hideLastSeparator그리고.hideLastSeparatorView.
"" " " " " " 를 설정합니다." 를 설정합니다.myTableView.hideLastSeparator = YES.
이 방법은 마지막 구분 기호 위에 새 보기를 추가하여 해당 구분 기호를 차단하는 것입니다.

은 ( 지정 구분자를 하거나 마지막 보다) 가 좀 쉽다고합니다. 셀하고, .tableFooterView원인이 될 수 있습니다(예: 행 삽입/삭제 또는 기타 테이블 애니메이션 중).

지정 기호를 UITableViewCell최상의 솔루션은 다음과 같습니다.

  1. 셀에서 용자지셀기구현다음기숨깁다능니을여를 숨기는 합니다.separatorView
class YourCell: UITableViewCell {
    // Your implementation goes here...
    // Separator view in cell
    @IBOutlet private weak var separatorView: UIView!

    // This function hides the separator view
    func hideSeparator() {
        separatorView.isHidden = true
    }
}
  1. tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)합니다.
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? YourCell else {
            return
    }
    // Here we're checking if your cell is the last one
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        // if true -> then hide it
        cell.hideSeparator()
    }
}

저는 Swift 5, iOS 15 및 xCode 13.3.1에서 테스트하고 작업 중입니다. 반환 셀 바로 앞에 있는 TableView "cellForRowAt" 메서드에서 이 코드를 붙여넣으면 프레임에서 마지막 줄이 밀립니다.참고:-("arrayCartItems"는 "numberOfRowsInSection"으로 반환하는 배열이 되며 "tblVw"는 ViewController에서 UITableView의 아웃렛 이름입니다) Newbies도 쉽게 배울 수 있도록 자세히 기록:)

    if indexPath.row == arrayCartItems.count - 1 {
        cell.separatorInset = UIEdgeInsets.init(top: 0, left: tblVw.bounds.width + 1, bottom: 0, right: 0)
    } else {
        cell.separatorInset = .zero
    }

가장 적합하고 쉬운 방법은 다음에서 사용하는 것입니다.cellForRowAt

cell.drawBottonLine = (indexPath.row != sections[0].rows.count - 1)

Tonny Xu의 답변을 확장하면, 저는 여러 섹션을 가지고 있으며 이것은 마지막 셀 분리기를 제거하는 데 효과가 있는 것 같습니다.

if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1)
{
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
}

에서cellForRowAtIndexPath데이터 소스

언급URL : https://stackoverflow.com/questions/12168002/how-to-remove-the-last-border-of-the-last-cell-in-uitableview

반응형