简体   繁体   中英

Hide/show cells in UITableView?

I have create a UITableView in IB. This view contains 5 sections and every section some cells. The first cell in some sections gives the option to the end user to show/hide the rest of the cells that belongs to the same section.

My code so far:

import UIKit

class SettingsVC: UITableViewController {

    @IBOutlet var showCallForwardSwitch: UISwitch?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func toggleValueChanged(sender: UISwitch) {

        if showCallForwardSwitch!.on {
            println("switch is on")
        } else {
            println("switch is off")
        }

        tableView.reloadData()
    }  
}

So there is only an IBOutlet and an IBAction. I can get the event via the toogleValueChanged func, however I don't know what to do from now on. Which methods to I need to use?

在此处输入图片说明

override func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
    let cell:DetailCell = self.tableView.cellForRowAtIndexPath(indexPath) as DetailCell // ????????

    var height:CGFloat = 84.0;

    if ("toggel on"){
        height = 84.0;
    }
    else{
        height = 0.0;
    }

    return height;
}

You don't wanna reload the whole tableView because of one section. I suggest using deleteRowsAtIndexPaths(_:withRowAnimation:) keeping only the first cell so the user will be able to display the rest of the section again. Update your data source as well and use some flag so you know which cell to not display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM