ios - 动态 UITableView 由同一 ViewController 中的第二个 UITableView 控制 - Swift

标签 ios uitableview swift

这是我昨天提出的一个问题的后续,我认为我有一个基于收到的答案的解决方案,但我遇到了新的困难。

我有一个 ViewController 和两个 UITableViews(PropertyTypeListPropertyDetailList) 我基本上想把它当作一个主细节 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/

相关文章:

swift - 要查询或数据库规则的 Firestore 模型组/成员

ios - 使用 Swift 进行卡片查看

ios - 适用于 UITableViewDataSource 的数据表示

ios - Mapbox iOS8 Swift mapView.showUsersLocation

ios - 生成新的 APNS 证书是否会使现有的事件证书失效

ios - 从所有(可见和不可见)UICollectionViewCell 获取框架

ios - 如何从 json 响应创建动态节头

ios - iOS更改受UIVibrancyEffect影响的标签颜色

ios - 如何显示来自不同类的警报

ios - 消息发送到解除分配的实例 ARC