DEV Community

Kiwi
Kiwi

Posted on

Help needed ;-;

import UIKit

class ItemTableViewCell: UITableViewCell {

@IBOutlet weak var stepperText: UILabel!

@IBOutlet weak var priceLabel: UILabel!

var item : Item! // this is a view controller class that have the item data .. price as a double (var price: Double! )


override func awakeFromNib() {

    super.awakeFromNib()

}


IBAction func stepper(_ sender: UIStepper) {

    var item : Item! // I called it again bc it didn’t connect in the stepper func ;-;

    stepperText.text = String(Int(sender.value))

    priceLabel.text = String(Int(sender.value))

    let n = item.price // HERE is the error (Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value)

    let nn = Double(Double(stepperText.text! ?? “0”) ?? 0)

    priceLabel.text = “\(n! * nn )”

    // i made this code to take the item price and when I use the stepper it multiple the step number with the price ;0; help plz
}
Enter fullscreen mode Exit fullscreen mode

}

Discussion (5)

Collapse
alvaromontoro profile image
Alvaro Montoro

I cannot help you with the code, because I don't know Swift, but here are some recommendations to improve the article, so it gets more traction and more visibility:

  1. Add a meaningful title. Change the title from Help needed ;-; to a short sentence more descriptive of the problem. Examples of titles that could match the post better:

    • Unexpectedly found nil while implicitly unwrapping an Optional value
    • Error: The UIStepper function is not connected correctly

    Note: I don't know if those match the problem. Ultimately, it is you the one that knows the problem best, and the one that can pick a meaningful title for the post.

  2. Format the code. Use three backticks and the beginning and end and specify the language to add highlights, then it will be easier to read. Something like this:

    ```java
    // the java/swift code here
    ```
    

     
    The result will look like this:

    import UIKit
    
    class ItemTableViewCell: UITableViewCell {
    
        @IBOutlet weak var stepperText: UILabel!
        @IBOutlet weak var priceLabel: UILabel!
    
        var item : Item! // this is a view controller class that have the item data .. price as a double (var price: Double! )
    
        override func awakeFromNib() {
            super.awakeFromNib()
        }
    
        // ...
    

     

  3. Add a description of the problem and the error. Right now, the comments have a small version of that but they are difficult to read. Instead, write a couple of paragraphs before the code, with what you are trying to do and the error you are getting.

    Also (and I understand this may be a personal preference), in the description, avoid sentences like send teh codez! or help plz, they make it sound like you are not being serious, and may steer people away.

Hopefully with those three changes, your post gets more visibility and someone can help you solve the problem. Goof luck!

Collapse
lilkiwi profile image
Kiwi Author

Thank you 👉🏻👈🏻💞💝💓💞💕💓💞💓💕
That actually helps me a lot cuz it’s my first time posting here

Collapse
recursivefaults profile image
Ryan Latta

Okey dokey, its been a while here.

There're two things I need to point out.

  1. Your specific problem is because Item isn't initialized to anything. You're trying to access something that isn't there.

  2. You're building a UITableView which is something every iOS developer has to figure out and it takes a little bit before it makes sense. So here we go.

In your UITableViewDelegate and DataSource you have methods to create your UITableViewCell, I forget their name. Its got a line like... initializeTableCellWithIdentifier. In that same method you have to give the TableViewCell all the information it needs. Don't have your TableViewCell connect back to your controller. Pass the Item from your controller INTO the tableview cell

So in your DataSource/Delegate method you might do something like, cell.setItem(item).

The reason you need to have it work this way is that those TableViewCells get recycled. Most people don't realize this. But iOS only uses a few TableCells and when one scrolls off screen it gets marked for re-use. So you have to reset those cells in that method EVERY time. There's also another method you can use to optimize this like.. willAppear or something, but don't worry about that for now.

I believe in the documentation Apple provides they have a demo project for this that will be really helpful in getting this set up and showing you how to manipulate a UITableViewCell.

Collapse
lilkiwi profile image
Kiwi Author

Aaaaaa thank you a lot 💕💞💕💕 , I accessed the cell informations but how do I initialize? 🥲🥲 what is the code + I got a lot of questions srry for bother

Collapse
lilkiwi profile image
Kiwi Author

I’m STUCK with this
.. ;-;
Help plz iOS developers