개발

iOS UITableView Section과 Section 사이 공백 제거 방법

소소ing 2024. 3. 26. 12:45
반응형

UITableView에 Section으로 영역을 나누어 사용을 하려고 했다.

각 Section 안 HeaderView와 Cell로 구성을 하려고 코드를 구성했다.

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SettingHeaderView") as? SettingHeaderView else { return nil }
        headerView.model = .init(section == 0 ? "test1" : "test2")
        self.headerView = headerView
        return self.headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 28
    }
    // ... 생략 .... 
 

 

실행을 하고 보니 Section과 Section 사이 공백이 생성된 것이 보였다.

왜 이럴까?

 

결론은 Default FooterView가 존재해서 인듯 하다.

아래 코드로 FooterView를 빈뷰로 설정하고 높이를 0으로 만들면 해결!

    // FooterView에 빈 뷰를 반환하여 공백을 제거
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    // FooterView에 빈 뷰 반환 후 높이 지정해서 지정대로 움직인다.
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
        return 0
    }
 
 

 

 

반응형