Ios Swift UITableView更新节标题和行内容

Ios Swift UITableView更新节标题和行内容,ios,uitableview,swift,Ios,Uitableview,Swift,我有一个包含按钮和表格视图的应用程序,我想一旦我点击一个按钮,章节标题就会改变,内容也会改变,我不想使用太多的章节或太多的行。 只有一个部分和一行可以在每次单击时更改其内容。问题是它不显示,而是显示“测试”作为部分标题,而不显示其他内容。 以下是我迄今为止所尝试的: var key = Int() func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {

我有一个包含按钮和表格视图的应用程序,我想一旦我点击一个按钮,章节标题就会改变,内容也会改变,我不想使用太多的章节或太多的行。 只有一个部分和一行可以在每次单击时更改其内容。问题是它不显示,而是显示“测试”作为部分标题,而不显示其他内容。 以下是我迄今为止所尝试的:

    var key = Int()
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
    // do not display empty `Section`s

    if(key == 1) {
        return "Monthly Usage"
    }
    if(key == 2) {
        return "Monthly Remaining"
    }
    if(key == 3) {
        return "Current Package"
    }
    return "Test"
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let row = indexPath.row
    // Configure the cell...

    switch(key) {
    case 1:
        cell.textLabel?.text = Data["Monthly_Usage"]
    case 2:
        cell.textLabel?.text = Data["Monthly_Remaining"]
    case 3:
        cell.textLabel?.text = Data["Monthly_Max"]
    default:
        cell.textLabel?.text = ""
    }
    return cell
}


@IBAction func Monthy_Usage() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 1
    }
    else {
        key = 1
    }
    println(key)
}
@IBAction func Monthy_Remaining() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 2

    }
    else {
        key = 2
    }
    println(key)
}
@IBAction func Current_Package() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 3
    }
    else {
        key = 3
    }
    println(key)
}
更新:


刚刚解决了这个问题,我必须在每个iAction函数中执行tableView.reloadData()

我忘记了每次单击都需要重新加载数据,所以我就是这样解决的:

    @IBAction func Monthy_Usage() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 1
    }
    else {
        key = 1
    }
    tableView.reloadData()
    println(key)
}
@IBAction func Monthy_Remaining() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 2

    }
    else {
        key = 2
    }
    println(key)
    tableView.reloadData()
}
@IBAction func Current_Package() {
    if tableView.hidden == true {
        tableView.hidden = false
        key = 3
    }
    else {
        key = 3
    }
    println(key)
    tableView.reloadData()
}

在“cellForRowAtIndexPath”中设置默认文本。在你的开关中它总是默认值吗,case fun.@iravivooda是的。