D. Lin
D. Lin

Reputation: 73

How to put string from array into variable using UITableView Swift

I want to put the string from whichever cell is chosen (part of the arrat basicPhrases) into a variable (selectedBPhrase) then I want to perform the segue BasicPhrases2Phrase and have it display the string by itself. How do I do that? I'm pretty sure it has something to do with using didSelectRowAtIndexPath but I'm not sure.

This is my code:

import UIKit

class BasicPhrases: UITableViewController {

let basicPhrases = ["Hello.","Goodbye.","Yes.","No.","I don't understand.","Please?","Thank you.","I don't know."]
var selectedBPhrase = ""

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return basicPhrases.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    cell.textLabel?.text = basicPhrases[indexPath.row]
    return cell
}

I think it's in here but not sure.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("BasicPhrases2Phrase", sender: self)
}
}

Thanks for any help in advance.

Upvotes: 0

Views: 124

Answers (2)

Bettimms
Bettimms

Reputation: 671

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
 {
      selectedBPhrase =  basicPhrases[indexPath.row]
     performSegueWithIdentifier("BasicPhrases2Phrase", sender: self)
}

Upvotes: 3

dimpiax
dimpiax

Reputation: 12677

You need to get UIViewTableCell from indexPath, as in cellForRowAtIndexPath, as in didSelectRowAtIndexPath.

Upvotes: 0

Related Questions