【发布时间】: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