46828

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

Question:

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 propertyTypeList, then have propertyDetailView populate based upon the selection in propertyDetailList.

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.

here is my code:

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

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

Answer1:

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

<blockquote>

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.

</blockquote>

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.

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.

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

Recommend

  • Cannot add fields when importing CSV to a table in BigQuery
  • How to solve the error when file doesn't exist in setting coordinator oozie
  • Java Import Package (to package above present work directory)
  • Loading dynamic images from SQLite
  • how to parsing data json from javascript to php
  • fragment with custom arrayadapter and layout with multiple elements
  • Getting keys from Firebase query
  • Matlab: 3D stacked bar chart
  • Spark scala: SELECT in a foreach loop returns java.lang.NullPointerException
  • How do I convert Rtf to Text using ASP.Net?
  • How to get elements with a unique number from a json array in PostgreSQL?
  • Turn list output into an array of objects or something similar using Powershell
  • How to separate Sphinx chapters within a single part into different files?
  • Overriding vals in Scala
  • How to concatenate two or more arrays in PHP without loosing values if it is same key and different
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • How can I add web content type in liferay 6?
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • testing a POST using phpunit in laravel 4
  • Aptana 3 remove bundle (jquery)
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • Uncaught TypeError: $(…).select2 is not a function
  • Using Sax parsing to edit and write XML in VB6
  • bad substitution shell- trying to use variable as name of array
  • Django: Count of Group Elements
  • Sending data from AppleScript to FileMaker records
  • SSO with signing and signature validation doesn't work
  • MySQL WHERE-condition in procedure ignored
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?