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

时间:2015-05-06 02:19:25

标签: ios uitableview swift

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

我有一个ViewController,其中包含两个UITableViewsPropertyTypeListPropertyDetailList) 我基本上希望将此视为主细节视图,当选择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 {
    }

}

1 个答案:

答案 0 :(得分:1)

只需致电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 {
}

}