这是我昨天提出的一个问题的后续,我认为我有一个基于收到的答案的解决方案,但我遇到了新的困难。
我有一个 ViewController
和两个 UITableViews
(PropertyTypeList
和 PropertyDetailList
)
我基本上想把它当作一个主细节 View ,当 PropertyTypeList 中的一行被选中时,PropertyDetailList 会根据该选择更新到一个数组。 (我需要它在一个 ViewController 中)
问题是我无法将从 didSelectRowAtIndexPath
中选择的行放入 cellForRowAtIndexPath
中,我通过 If 语句填充表以确定正在播放的是哪个 TableView 。
我现在通过变量 (currentRowInTypeList
) 获取当前选定的行,并使用该值来切换将哪个数组加载到 PropertyDetailList
。
尽管 currentRowInTypeList
会随着每次点击而更新,但表格不会。我错过了什么?
我的代码如下:
@IBOutlet weak var propertyTypeList: UITableView!
@IBOutlet weak var propertyDetailList: UITableView!
var testarray = ["test1", "test2"]
var testarray2 = ["test3", "test4"]
var currentRowInTypeList: Int = 0
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
if tableView == propertyTypeList {
return projectSources.count;
}
else {
if currentRowInTypeList == 0 {
return testarray.count
} else {
return testarray2.count
}
}
}
func tableView(tableView: UITableView!,
cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
if tableView == propertyTypeList {
cell.textLabel?.text = projectSources[indexPath.row]
return cell
}
else
{
if currentRowInTypeList == 0 {
cell.textLabel?.text = testarray[indexPath.row]
return cell
} else {
cell.textLabel?.text = testarray2[indexPath.row]
return cell
}
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
if tableView == propertyTypeList {
currentRowInTypeList = indexPath.row
println(currentRowInTypeList)
} else {
}
}
最佳答案
只需调用propertyDetailList.reloadData()
在您的代码中:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
if tableView == propertyTypeList {
currentRowInTypeList = indexPath.row
propertyDetailList.reloadData()
println(currentRowInTypeList)
} else {
}
关于ios - 动态 UITableView 由同一 ViewController 中的第二个 UITableView 控制 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066357/