Skip to content

Instantly share code, notes, and snippets.

@TuenTuenna
Last active September 3, 2021 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TuenTuenna/60ef7324caf3a8774e21ab44c81878bf to your computer and use it in GitHub Desktop.
Save TuenTuenna/60ef7324caf3a8774e21ab44c81878bf to your computer and use it in GitHub Desktop.
expy tableview 토글 형식 구현

ExpyTableView 토글 구현

extension NoticeViewController: ExpyTableViewDelegate {
    func tableView(_ tableView: ExpyTableView, expyState state: ExpyState, changeForSection section: Int) {
        print("NoticeViewController - ExpyTableViewDelegate - changeForSection() called - section = \(section)")
        
        switch state {
        case .willExpand:
            print("펼쳐질 꺼다/ .willExpand / section: \(section)")
            
            print("현재 섹션: \(section), tableView.expandedSections : \(tableView.expandedSections)")
            
            // 접어야 할 섹션들
            let expandedSections = tableView.expandedSections.map{ $0.key }.filter{ $0 != section }
            
            print("willExpand 현재 들어온 섹션: \(section), 펼쳐진 섹션: expandedSections: \(expandedSections)")
             
        case .willCollapse:
            print("닫힐 꺼다 /.willCollapse / section: \(section)")
        case .didExpand:
            print("펼쳐짐 / .didExpand / section: \(section)")
            // 접어야 할 섹션들
            
            let expandedSections = tableView.expandedSections.map{ $0.key }.filter{ $0 != section }

            if expandedSections.count > 0 {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.02, execute: {
                    expandedSections.forEach{ tableView.collapse($0) }
                })
            }
            print("didExpand 현재 들어온 섹션: \(section), 펼쳐진 섹션: expandedSections: \(expandedSections)")
        case .didCollapse:
            print("닫힘 /.didCollapse / section: \(section)")
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment