heoblitz
Blitz.dev
heoblitz
전체 방문자
오늘
어제
  • 분류 전체보기 (36)
    • iOS Dev (22)
      • iOS (3)
      • Swift (7)
      • Testing (3)
      • Reactive (2)
      • Architecture (2)
      • Layout (1)
    • PS (4)
      • Algorithm (4)
    • Other (9)
      • Springboot (3)
      • Linux (1)
      • Python (1)
      • Java (1)
      • React (1)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • intellij
  • swift 5
  • java
  • SWIFT
  • XCTest
  • Test Code
  • github
  • IOS
  • RxSwift
  • swift 윈도우
  • Code Review
  • URLSession
  • springboot
  • ARC
  • chrome-extension
  • gradle
  • codingtest
  • 오픈소스
  • Git
  • xcode

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
heoblitz

Blitz.dev

iOS "UITableView Section Header 로드되지 않는 문제" 해결
iOS Dev/Swift

iOS "UITableView Section Header 로드되지 않는 문제" 해결

2020. 7. 26. 16:57

Xcode 11.6 - iOS 13, Swift 5

 

위 영상처럼 처음 로드했을 때에는 섹션 헤더가 제대로 보이지만, ( xib 를 사용한 Custom Section Header )

 

나중에 해당 섹션만 reload 했을 때 Section Header 가 보이지 않는 문제가 발생했습니다.

 

 

 

Stack Overflow 에 나와있는 일반적인 해결 방법은 아래와 같습니다.

 

1. UITableViewDelegate 프로토콜에서 heightForHeaderInSection 리턴하기.

 

2. reloadRows 대신 reloadSection 으로 해당 섹션 리로드 하기

 

 

제 경우에는 위 두가지 방식으로도 해결되지 않아서 구글링을 하며 더 찾아보았습니다. 

 

// UITableView Delegate
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    switch section {
    case 3:
        guard let sectionHeader = tableView.dequeueReusableCell(withIdentifier: DetailMovieCommentNavigationCell.cellIdentifier) as? DetailMovieCommentNavigationCell  else {
            return nil
        }

        sectionHeader.editButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
        
        // return sectionHeader
        return sectionHeader.contentView
    default:
        return nil
    }
}

 

해결 방법은 "viewForHeaderInSection에서 cell 의 contentView 를 리턴하는 것입니다."

 

 

일반적으로 Cell 혹은 Cell as UIView 로 다운 캐스팅하여 리턴해도 문제가 없지만, 

 

특정 섹션만 리로드 해야하는 경우, Cell.contentView 를 리턴해주면 제대로 동작되는 것을 볼 수 있습니다.

 

 

 

 

 

 

* 추가 : iOS 개발을 얼마 배우지 않고 작성한 글이라 틀린 부분을 추가합니다.

 

TableView 에서 Header 혹은 Footer View를 사용할 때 TableViewCell 로 서브클래싱하는 것이 아니라,

 

UITableViewHeaderFooterView 로 서브클래싱하는 것이 올바른 방법입니다.

 

 

참고:
https://stackoverflow.com/questions/23424951/uitableview-section-header-disappears-if-i-insert-or-delete-rows/45925885

'iOS Dev > Swift' 카테고리의 다른 글

iOS JSON 을 Decoding 하는 여러 가지 방식들  (0) 2021.08.26
지금까지 받았던 코드 리뷰 정리 ( iOS, Swift )  (0) 2020.11.10
Swift Optional ( enum, wrapped ) 정리  (0) 2020.10.12
Swift ARC ( Automatic Reference Counting ) 정리  (2) 2020.10.07
윈도우 10 에서 Swift 5 사용하기 ( Visual Code, WSL )  (4) 2020.08.01
    'iOS Dev/Swift' 카테고리의 다른 글
    • 지금까지 받았던 코드 리뷰 정리 ( iOS, Swift )
    • Swift Optional ( enum, wrapped ) 정리
    • Swift ARC ( Automatic Reference Counting ) 정리
    • 윈도우 10 에서 Swift 5 사용하기 ( Visual Code, WSL )
    heoblitz
    heoblitz
    iOS, Swift 관련 포스팅을 주로 작성합니다.

    티스토리툴바