Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Collapsable UITableView header in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.87k
    Comment on it

    To collapse table header in swift ->

    1. Make a property of NSMutableSet, name it "collapsedSections", this property will be use to check Collapsed section in the UItableViewCell.

     

    var collapsedSections:NSMutableSet = []

     

    2. Create a header for UITableView and give UIButton tag to the header .

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let view = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 25))
          view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.9)
            
          let headerLbl = UILabel(frame: CGRectMake(12, 4, tableView.bounds.size.width-12, 18))
          headerLbl.font = UIFont(name: "HelveticaNeue", size: 16)
          headerLbl.tag = 111
          headerLbl.textColor = UIColor.whiteColor()
           if section == 0{
                   headerLbl.text = Section1
                 }
            else{
                   headerLbl.text = Section2
                }
           let btn = UIButton(frame: CGRectMake(0, 0, tableView.bounds.size.width, 25))
    
    // Add action to UIButton
           btn.addTarget(self, action: "tableHeaderTouched:", forControlEvents: UIControlEvents.TouchUpInside)
           btn.tag = section
           view.addSubview(btn)
           view.addSubview(headerLbl)
           return view
        }

     

    3. To Collapse section of UITableView the following method will be trigger .

    func tableHeaderTouched(sender : UIButton){
            tableView.beginUpdates()
            var section = Int()
            section = sender.tag
            var shouldCollapse = Bool()
            shouldCollapse = !collapsedSections.containsObject(section)
            var numOfRows = Int()
            var indexPaths = NSArray()
            if shouldCollapse{
    
    // this condition will add row to cell
                numOfRows = self.tableView .numberOfRowsInSection(section)
                indexPaths = self.indexPathsForSection(section, withNumberOfRows: numOfRows)
                self.tableView .deleteRowsAtIndexPaths(indexPaths as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Top)
                collapsedSections.addObject(section)
                
            }
            else{
    
    // this condition will delete row from cell
                indexPaths = self.indexPathsForSection(section, withNumberOfRows: numOfRows)
                self.tableView.insertRowsAtIndexPaths(indexPaths as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Top)
                collapsedSections.removeObject(section)
            }
            self.tableView .endUpdates()
        }

     

    4. This function will calculate indexPath of section to add/delete the rows in UITableView header .

    func indexPathsForSection(section:Int, withNumberOfRows numberOfRows:Int)->NSArray{
            let indexPaths = NSMutableArray()
            var indexPath = NSIndexPath()
            
            var i = Int()
            for i = 0; i < numberOfRows; i++ {
                indexPath = NSIndexPath(forRow: i, inSection: section)
                indexPaths.addObject(indexPath)
            }
            return indexPaths
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: