根据 swift 中选择的第一个 table 行更改第二个 table 内容

Change second table content based on first table row selected in swift

我有一个包含两个表(propertyTypeList 和 propertyDetailList)和一个文本视图(propertyDetailView)的视图

我想做的是根据在 propertyTypeList 中所做的选择使用数组填充 propertyDetailList,然后根据 propertyDetailList 中的选择填充 propertyDetailView。

我可以使用底部函数和 indexPath.row 查看当前选定的行,但我无法在上面的函数中调用该函数。

这是我的代码:

class NewProjectView: UIViewController {


@IBOutlet weak var propertyTypeList: UITableView!
@IBOutlet weak var propertyDetailList: UITableView!
@IBOutlet weak var propertyDetailView: UITextView!

var testarray = ["test1", "test2"]

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
    if tableView == propertyTypeList {
    return projectSources.count;
    }
    else {
        return testarray.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]

        if indexPath.row == 0 {
            println("Row 0")
            }
        else
            {
                println("Not Row 0")
            }

        return cell
        }
    else
        {
        cell.textLabel?.text = testarray[indexPath.row]

        return cell
        }

}



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    println(indexPath.row)


}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

如何从第二个 tableView Func 访问 indexPath.row?

你的问题好像有错别字。你说:

I have a view with two tables (propertyTypeList & propertyDetailList) and a text view (propertyDetailView)

What I'm trying to do is have propertyDetailList populate with an array based upon the selection made in propertyDetailList, then have propertyDetailView populate based upon the selection in propertyDetailList.

你是不是想说 "What I'm trying to do is have propertyDetailList populate with an array based upon the selection made in propertyTypeList..." 这样更有意义。

所以您基本上有一个主从设置,其中 propertyTypeList 是主 table 视图,propertyDetailList 是详细 table 视图。

我假设 table 视图的数据源和委托都指向同一个视图控制器。

您需要做的是编写 tableView:didSelectRowAtIndexPath: 方法来检查 tableView 参数。当用户在任一 tableView 中选择一行时将调用该方法,但 tableView 参数可让您确定所选单元格属于哪个 table 视图。

因此您的代码可能如下所示:

func tableView(
  tableView: UITableView, 
  didSelectRowAtIndexPath indexPath: NSIndexPath) 
{
  if (tableView == propertyTypeList)
  {
    //Switch the selected item in the detail list
  }
  else
  {
    //do whatever is appropriate for selecting a detail cell.
  }
}

关于您对@Duncan C 的回答@BlueRad 的评论,只需在 if 语句中重新加载您的第二个 tableView 数据

propertyDetailList.reloadData()

这样就可以了。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == propertyTypeList {
        self.propertyDetailList.reloadData()
    }
}