Reputation: 485
So, I have a UITableView established in my ViewController and the UITableView values are populated using an array. Whenever a user touches one of the cells they are suppose to be taken to another view controller. Well, when the app loads and the first cell is clicked on nothing happens, if the second cell is clicked on then the view controller associated with the first cell appears, and then it just goes downhill from there. I can't seem to find a solution to my problem, so I thought I'd ask people who might be able to point me in the right direction. Here is the associated code. Thank you in advance!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private let choices = ["About this App", "Touch Counting", "Slider Counting", "Calculator", "Date Picker"]
private let simpleTableID = "simpleTableID"
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return choices.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableID)
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: simpleTableID)
}
cell?.textLabel?.text = choices[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
switch(indexPath.row) {
case 0:
let vc = storyboard?.instantiateViewControllerWithIdentifier("AboutThisApp")
presentViewController(vc!, animated: true, completion: nil)
case 1:
let vc = storyboard?.instantiateViewControllerWithIdentifier("TouchCounting")
presentViewController(vc!, animated: true, completion: nil)
case 2:
let vc = storyboard?.instantiateViewControllerWithIdentifier("SlidingCounter")
presentViewController(vc!, animated: true, completion: nil)
case 3:
let vc = storyboard?.instantiateViewControllerWithIdentifier("Calculator")
presentViewController(vc!, animated: true, completion: nil)
case 4:
let vc = storyboard?.instantiateViewControllerWithIdentifier("Date")
presentViewController(vc!, animated: true, completion: nil)
default:
NSLog("NEVER!")
}
}
}
Upvotes: 0
Views: 36
Reputation: 7634
Change:
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
to:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
didSelectRowAtIndexPath
is called when the user selects a table view row, but didDeselectRowAtIndexPath
is called when a row is deselected, which happens to the first row once the user taps the second row.
Upvotes: 2