Showing and hiding rows in a iOS tableView

So I would like to use a tableView to show a list of controls

On one row I would like a date, and on the next row I’d like a CalendarPicker. I want the calendarPicker row to appear or disappear when I click on the date row. This looks like a way to handle it:

 var expandedRow = -1; //sentinal value
    var heights:[CGFloat] =  [44.0,44.0]
    let HiddenRowHeight: CGFloat = 0.0
    let ExpandedRowHeight: CGFloat = 88.0
    let DefaultHeight: CGFloat = 44.0
   
    
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  
        //assume row  0 shows the date
        //assume row 1 shows a Calendar picker
        var currentRow: Int = indexPath.row
        
        if(currentRow == 0) //assume row 0 is the row with a date
        {
            //if the calendar is showing, hide it.
            //if it is hidden, show it
             if expandedRow == currentRow + 1
             {
                //shrink it
                heights[currentRow + 1] = HiddenRowHeight
                expandedRow = -1
             }
            else
             {
                //expand it
                heights[currentRow + 1] = ExpandedRowHeight
                 expandedRow = currentRow + 1
             }
        }
        tableView.beginUpdates()
        tableView.endUpdates()
    }
    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    
        //get the height of the row from the array of heights
        var height = DefaultHeight
        var row = indexPath.row
        
        if heights.count > row
        {
            height = heights[row]
        }
        return height
    }
    

        

I’ll refine this article after I implement the screen.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s