Hide/show cells in UITableView?

时间:2015-06-25 09:57:49

标签: ios swift uitableview cocoa-touch

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?

enter image description here

2 个答案:

答案 0 :(得分:1)

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;
}

答案 1 :(得分:0)

由于一个部分,您不想重新加载整个tableView。我建议使用deleteRowsAtIndexPaths(_:withRowAnimation:)只保留第一个单元格,这样用户就可以再次显示该部分的其余部分。同时更新您的数据源并使用一些标志,以便您知道哪个单元格不显示。