1
\$\begingroup\$

Is there a better way to write this? This is a class that displays a preferences menu. We do this when the user first installs the app during a walk-through where we only show them some of the settings with additional guidance on how to set them. We also show the entire preferences menu if the user selects our preferences button.

    /**
 Manages the height of the table's cells, hiding them if necessary.

 parameter: The table we'll edit
 parameter: The height in dpi of the row at a given row
*/

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch walkthroughStep {

        // step 1 of the walkthrough
        case 1:


            switch indexPath.row {
            case 0: return 0.1
            case 3:
                if ((promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize) > 350){
                    //print ("the result of this is:  ---------- \(promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize)")
                    return 80
                }
                else { return 50.0 }
            case 4: return 9.0
            case 5: return 0.1
            case 6: return 0.1
            case 7: return 0.1
            case 8: return 0.1
            case 9: return 0.1
            case 10: return 0.1
            case 11: return 0.1
            default: break
        }

        // step 2 of the walkthrough
        case 2: switch indexPath.row {
            // hide all cells of teh default methos table
            case 0: return 0.1
            case 1: return 0.1
            case 2: return 0.1
            case 3: return 0.1
            case 4: return 0.1
            case 5:
                if enableNotifications.on {
                    return selectedCellHeight
                }
                return unselectedCellHeight
            case 6: return 9.0
                // hide propmt me to modify number cell
            case 7: return 0.1
            case 8: return 0.1
            case 9: return 0.1
            case 10: return 0.1
            case 11: return 0.1
            default: break
        }

        // normal state
        default:
            switch indexPath.row {
            // hide all cells of teh default methos table
            case 0: return 50.0
            case 1: return 50.0
            case 2: return 50.0
            case 3:
                if ((promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize) > 350){
                    //print ("the result of this is:  ---------- \(promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize)")
                    return 80
                }
                else { return 50.0 }
            case 4: return 9.0
            case 5:
                if enableNotifications.on {
                    return selectedCellHeight
                }
                return unselectedCellHeight
            case 6: return 9.0
                // hide propmt me to modify number cell
            case 7:
                if ((modifyNumbersLabel.intrinsicContentSize().height * modifyNumbersLabel.font.pointSize) > 310){
                    //print ("the result of this is:  ---------- \(modifyNumbersLabel.intrinsicContentSize().height * modifyNumbersLabel.font.pointSize)")
                    return 90
                }
                else { return 50.0 }
            case 8: return 9.0
            case 9: return 50.0
            case 10: return 50.0
            case 11: return 50.0
            default: break
        }
    }

    return 50.0
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I would split the table into multiple sections and set the number of rows to 0 for those sections I do not want to display and only use the default settings in heightForRowAtIndexPath

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    switch indexPath.section {
    // hide all cells of teh default methos table
    case 0: return 50.0
    case 1:
        if ((promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize) > 350){
            //print ("the result of this is:  ---------- \(promptLabel.intrinsicContentSize().height * promptLabel.font.pointSize)")
            return 80
        } else { return 50.0 }
    case 2: return 9.0
    case 3:
        if enableNotifications.on {
            return selectedCellHeight
        }
        return unselectedCellHeight
    case 4: return 9.0
    // hide propmt me to modify number cell
    case 5:
        if ((modifyNumbersLabel.intrinsicContentSize().height * modifyNumbersLabel.font.pointSize) > 310){
            //print ("the result of this is:  ---------- \(modifyNumbersLabel.intrinsicContentSize().height * modifyNumbersLabel.font.pointSize)")
            return 90
        } else { return 50.0 }
    case 6: return 9.0
    case 7: return 50.0
    default: break
    }

return 50.0
}

And implement these functions:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 8
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0: 
        // Original Rows 0, 1, 2
        switch walkthroughStep {
        case 1,2: return 0
        default: return 3
        } 
    case 1:
        // Original Row 3
        switch walkthroughStep {
        case 2: return 0
        default: return 1
        }  
    case 2:
        // Original Row 4
        switch walkthroughStep {
        case 2: return 0
        default: return 1
        }  
    case 3:
        // Original Rows 5
        switch walkthroughStep {
        case 2: return 0
        default: return 1
        }  
    case 4:
        // Original Row 6
        switch walkthroughStep {
        case 1: return 0
        default: return 1
        }  
    case 5:
        // Original Row 7
        switch walkthroughStep {
        case 1,2: return 0
        default: return 1
        } 
    case 6:
        // Original Row 8
        switch walkthroughStep {
        case 1,2: return 0
        default: return 1
        } 
    case 7:
        // Original Rows 9, 10, 11
        switch walkthroughStep {
        case 1,2: return 0
        default: return 5
        }  
     default:
         return 0
    } 

So most of the display logic is in the numberOfSectionsInTableView method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.