1

I'm currently making an app for fun that takes the price you want and shows you a list of PC parts that you can buy in your budget.

I have a UI Slider in a View Controller that sets the price and then that price is segued to a Table View Controller; this price that is segued determines what parts show in the cells.

The issue is that it displays a large random number for an unknown reason . Which messes up the app because the UITableViewController cant receive the right number and doesn't display the build.

Here's a recent build output. the 1950 and 2000 are UISlider values and 2061676976 is the number the segue prints

1950
1950
2000
2000
2000
2000 <- the next number should be this
2061676976 < - but instead it prints this

Here's the UIViewController:

import UIKit


class ViewController: UIViewController {

@IBOutlet var priceText: UILabel!
@IBOutlet var spendText: UILabel!
@IBOutlet var priceSlider: UISlider!
@IBOutlet var segueButton: UIButton!

var price = 1000
let step: Float = 50
var stringPrice = ""


override func viewDidLoad() {Nothing useful in here, just animations}

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

@IBAction func choosePrice(sender: UISlider) {
    //let roundedValue = round(sender.value / step) * step
   let roundedValue = sender.value

    price = Int(roundedValue)
    stringPrice = "$" + String(price)

    priceText.text = stringPrice

    print(price)


}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    print(price)
    if segue.identifier == "buildSegue" {
        let destinationController = segue.destinationViewController as! PCBuildTableViewController
        destinationController.priceOfBuild = price

         }

    }

}

The UITableViewController looks like this:

import UIKit

class PCBuildTableViewController: UITableViewController {

var priceOfBuild = 0

var TwoKBuildImg = ["2KCPU", "2KFan", "2KMobo", "2KRAM", "2KSSD", "2KHDD", "2KGPU1", "2KGPU2", "2KCase", "2KPSU"]
var TwoKBuildName = ["Intel Core i5-6600K 3.5GHz CPU", "Cooler Master Hyper 212 EVO", "MSI Z170A-G45 GAMING", "Kingston HyperX Fury Black 8GB", "Crucial BX100 250GB SSD", "Seagate Barracuda 1TB HDD", "EVGA GeForce GTX 980Ti (SLI)", "EVGA GeForce GTX 980Ti (SLI)", "NZXT S340 White Mid Tower", "EVGA 850 Watt 80+ Gold PSU"]

override func viewDidLoad() {
    super.viewDidLoad()
    //priceOfBuild = price
    print(priceOfBuild)
}

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

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

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "PCCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,
            forIndexPath: indexPath) as! PCTableViewCell

        if (priceOfBuild == 2000){
            cell.partImage.image = UIImage(named: TwoKBuildImg[indexPath.row])
            cell.partName.text = TwoKBuildName[indexPath.row]
            return cell
        }
        else { //If it fails to show $2000 build then I made it show an incorrect table here
            cell.partImage.image = UIImage(named: TwoKBuildImg[0])
            cell.partName.text = TwoKBuildName[0]
            return cell
        }

    }

}

Thanks in advance guys!

  • Can you share the code that performs the segue? Or is the button connected to the segue in the storyboard? – paulvs May 09 '16 at 16:58
  • I tried out your code and it's not generating the error. Must be something in your storyboard, viewDidLoad, or PCTableViewCell code. – Curmudgeonlybumbly May 09 '16 at 18:47

0 Answers0