0

I just spent like two hours googling/stack overflowing trying to find a solution to no avail. I am using a view controller inside of which I have a tableview. It populates just fine. When you click on one cell, I want it to go to a different ViewController and load more information about that cell's contents.

override func prepareForSegue(segue: UIStoryboardSegue, sender: 
  AnyObject?) {
    //This line keeps giving me an error - idk how to fix it
    //UITablview(numberOfRowsInSection:Int)->'Int' does not have a member named  'indexPathForSelectedRow()'

    var indexPath = tableView.indexPathForSelectedRow() 
    let AddPreferenceViewController : betatry1.AddPreferenceViewController
     = segue.destinationViewController as betatry1.AddPreferenceViewController

    AddPreferenceViewController.pref = preferences[indexPath]
  }

I initialized pref in AddPreferenceViewController so that's not the problem

here are the other two required tableView methods

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //println(curUser["preferences"])
   // tableView.rowHeight = UITableViewAutomaticDimension;
  //  tableView.estimatedRowHeight = 44.0;
    var preferences : [String] = curUser["preferences"] as [String]
            return preferences.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as UITableViewCell
    var preferences : [String] = curUser["preferences"] as [String]
    current = preferences[indexPath.row]
    cell.textLabel?.text = current
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    return cell
}

I basically need to pass the string at the index of which cell the user click to the AddPreferenceViewController.

Thanks for the help in advance

user3739383
  • 37
  • 1
  • 8
  • It looks like `preferences` is an array and you're using a `NSIndexPath` to index it. You must use an int to index an array. Try writing it as `preferences[indexPath.row]` – Neal Ehardt Mar 17 '15 at 18:33
  • The problem is that i am not able to get the IndexPath at all ,tableView.indexPathForSelectedRow() that line gives me an error. I think it's because I am not suing a TableView Controller and I don't how to go around that to get the index of the selected cell – user3739383 Mar 17 '15 at 18:34
  • Is `prepareForSegue` actually getting called? In general, I use `didSelectRowAtIndexPath` for handling a user click event. The latter will give you the `indexPath` you need. – Neal Ehardt Mar 17 '15 at 18:48
  • How do i go about using didSelectRowAtIndexPath? – user3739383 Mar 17 '15 at 18:51
  • https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/ – Neal Ehardt Mar 17 '15 at 18:57
  • I was able to get the index thanks! But now how do I pass it to the next view controller without prepareForSegue? – user3739383 Mar 17 '15 at 19:09
  • I'm guessing that you have a NavigationController in your stack - in that case, you can use http://makeapppie.com/2014/09/15/swift-swift-programmatic-navigation-view-controllers-in-swift/. If not, the fastest way is modal presentation http://stackoverflow.com/questions/24099533/swift-presentviewcontroller – Neal Ehardt Mar 17 '15 at 19:18
  • I still can't pass in the index as a member to the view controller though – user3739383 Mar 17 '15 at 19:36
  • Maybe you need to add a public property to your AddPreferenceViewController? https://developer.apple.com/swift/blog/?id=5 – Neal Ehardt Mar 17 '15 at 19:43

0 Answers0