Expand/collapse section in UITableView in iOS

前端 未结 17 936
野的像风
野的像风 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:52

    Expanding on this answer written in Objective C, I wrote the following for those writing in Swift

    The idea is to use sections within the table and set the number of rows in the section to 1 (collapsed) and 3(expanded) when the first row in that section is tapped

    The table decides how many rows to draw based on an array of Boolean values

    You'll need to create two rows in storyboard and give them the reuse identifiers 'CollapsingRow' and 'GroupHeading'

    import UIKit
    
    class CollapsingTVC:UITableViewController{
    
        var sectionVisibilityArray:[Bool]!// Array index corresponds to section in table
    
        override func viewDidLoad(){
            super.viewDidLoad()
            sectionVisibilityArray = [false,false,false]
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
        }
    
        override func numberOfSections(in tableView: UITableView) -> Int{
            return sectionVisibilityArray.count
        }
        override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
            return 0
        }
    
        // numberOfRowsInSection - Get count of entries
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            var rowsToShow:Int = 0
            if(sectionVisibilityArray[section]){
                rowsToShow = 3 // Or however many rows should be displayed in that section
            }else{
                rowsToShow = 1
            }
            return rowsToShow
        }// numberOfRowsInSection
    
    
        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
            if(indexPath.row == 0){
                if(sectionVisibilityArray[indexPath.section]){
                    sectionVisibilityArray[indexPath.section] = false
                }else{
                    sectionVisibilityArray[indexPath.section] = true
                }
                self.tableView.reloadSections([indexPath.section], with: .automatic)
            }
        }
    
        // cellForRowAtIndexPath - Get table cell corresponding to this IndexPath
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            var cell:UITableViewCell
    
            if(indexPath.row == 0){
                 cell = tableView.dequeueReusableCell(withIdentifier: "GroupHeading", for: indexPath as IndexPath)
            }else{
                cell = tableView.dequeueReusableCell(withIdentifier: "CollapsingRow", for: indexPath as IndexPath)
            }
    
            return cell
    
        }// cellForRowAtIndexPath
    
    }
    

提交回复
热议问题