根据 swift 中选择的第一个表格行更改第二个表格内容 [英] Change second table content based on first table row selected in swift

查看:27
本文介绍了根据 swift 中选择的第一个表格行更改第二个表格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想要做的是根据在 propertyTypeList 中所做的选择,用一个数组填充 propertyDetailList,然后根据 propertyDetailList 中的选择填充 propertyDetailView.

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

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

I can view the currently selected row using my bottom function and indexPath.row, but I can't get that call to work in the function above.

这是我的代码:

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?

How do I access indexPath.row from the second tableView Func?

推荐答案

您的问题似乎有错别字.你说:

It looks like there is a typo in your question. You said:

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

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

我想要做的是让 propertyDetailList 填充一个基于在 propertyDetailList 中所做的选择的数组,然后有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.

您的意思是说我要做的是根据在 propertyTypeList 中所做的选择,让 propertyDetailList 填充一个数组..."这会更有意义.

Did you mean to say "What I'm trying to do is have propertyDetailList populate with an array based upon the selection made in propertyTypeList..." That would make more sense.

所以你基本上有一个主从设置,其中 propertyTypeList 是一个主表视图,propertyDetailList 是详细表视图.

So you essentially have a master-detail setup, where propertyTypeList is a master table view and propertyDetailList is the detail table view.

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

I'm assuming that both table views have their data source and delegate pointed to the same view controller.

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

What you need to do is write your tableView:didSelectRowAtIndexPath:method to check the tableView parameter. That method will be called when the user selects a row in either tableView, but the tableView parameter will let you figure out which table view the selected cell belongs to.

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

So your code might look something like this:

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.
  }
}

这篇关于根据 swift 中选择的第一个表格行更改第二个表格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆