【问题标题】:Swift UITableView Update Section Title and Row ContentSwift UITableView 更新部分标题和行内容
【发布时间】:2015-03-30 23:25:12
【问题描述】:

我有一个包含按钮和表格视图的应用程序,我希望一旦我点击一个按钮,部分标题就会改变,它的内容也会改变,我不想使用很多部分或很多数字的行。 只有一个部分和一行会在每次点击时更改其内容。问题是它不显示,而是将“测试”作为部分标题显示,仅此而已。 到目前为止,这是我尝试过的:

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

更新:

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

【问题讨论】:

  • 在 `cellForRowAtIndexPath' 中设置默认文本。它是否总是在你的开关中进入默认状态,很有趣。
  • @iRaviiVooda 确实如此。

标签: ios uitableview swift


【解决方案1】:

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

    @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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多