Expand/collapse section in UITableView in iOS

前端 未结 17 926
野的像风
野的像风 2020-11-22 08:49

Could somebody tell me the way to perform UITableView expandable/collapsible animations in sections of UITableView as below?

<

17条回答
  •  耶瑟儿~
    2020-11-22 09:33

    in support to @jean.timex solution, use below code if you want to open one section at any time. create a variable like: var expandedSection = -1;

    func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
        let collapsed = !sections[section].collapsed
        // Toggle collapse
        sections[section].collapsed = collapsed
        header.setCollapsed(collapsed)
        tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
        if (expandedSection >= 0 && expandedSection != section){
            sections[expandedSection].collapsed = true
            tableView.reloadSections(NSIndexSet(index: expandedSection) as IndexSet, with: .automatic)
        }
        expandedSection = section;
    }
    

提交回复
热议问题