0

I currently have a ViewController with a TableView inside it called SelectedListItemsViewController. This ViewController's TableView is populated by a custom cell class called SelectedListItemsTableViewCell.

I have an array of Realm Model Objects called selectedListItems, each of which has several properties. The SelectedListItemsTableViewCell populates the TableView with the listItem property of that indexPath.row's object, and each row has a UIStepper with a UILabel next to it that (as of now) shows UIStepper.value for each row. Ideally, the label will reflect the listItemWeight property of each row, and change it when incrementing or decrementing that row.

This is my custom cell:

class SelectedListItemsTableViewCell: UITableViewCell {

    @IBOutlet var selectedListItemLabel: UILabel!
    @IBOutlet weak var listItemWeightLabel: UILabel!
    @IBOutlet weak var stepperControl: UIStepper!

    @IBAction func stepperValueChanged(sender: UIStepper) {

        listItemWeightLabel.text = Int(sender.value).description

    }
}

And in my ViewController's cellForRowAtIndexPath, I've configured the cell like so:

    // Configure the cell...
    cell.selectedListItemLabel.text = selectedListItems[indexPath.row].listItem

    cell.listItemWeightLabel.text = "\(selectedListItems[indexPath.row].listItemWeight)"

Which perfectly loads the listItem property, and the listItemWeight property shows up correctly, but as soon as I increment or decrement on the UIStepper it gets messed up.

How do I properly link my UILabel and UIStepper to the [indexPath.row].listItemWeight?

MachTurtle
  • 220
  • 3
  • 10

2 Answers2

0

In the same method that gets called when your stepper updates, update your listItem. However, since this item is stored in your Realm database, you will have to get an instance of your Realm database and write the change to the database.

You can do this by having your TableViewCell hold on to an instance of the listItem. Your new TableViewCell class will look something like this:

class SelectedListItemsTableViewCell: UITableViewCell {

    @IBOutlet var selectedListItemLabel: UILabel!
    @IBOutlet weak var listItemWeightLabel: UILabel!
    @IBOutlet weak var stepperControl: UIStepper!

    var listItem: Item?

    @IBAction func stepperValueChanged(sender: UIStepper) {
        listItemWeightLabel.text = Int(sender.value).description
        if let listItem = listItem {
            let realm = try! Realm
            try! realm.write {
                listItem.listItemWeight = Int(sender.value) ?? 0
            }
        }
    }
}
Nathan Ansel
  • 376
  • 3
  • 10
  • Thanks for the input, Nathan! I'm not able to update listItem in the method where the UIStepper updates (stepperValueChanged) since, as far as I'm aware (though that's not saying much since I'm very new to programming) it requires an indexPath.row to access that particular object's properties, which I don't have in the custom cell class. Am I wrong on that front? Any time I try to drill down to selectedListItems' properties, I get the warning "Value of type '[RealmListItem]' has no member 'listItemWeight'" – MachTurtle Sep 26 '16 at 20:14
  • if your `SelectedListItemsTableViewCell` has a reference to the listItem (from that array) than you should be able to update it. – Nathan Ansel Sep 26 '16 at 20:19
  • Apologies if this is a horribly naive question, but how would I go about referencing my array (it's global) from my `SelectedListItemsTableViewCell` in such a way that I could update the object in that row's properties? I've been searching around for the last 20 minutes but Google is interpreting 'reference' in a different fashion than we mean here. – MachTurtle Sep 26 '16 at 20:46
  • I was going to write a solution here, but Nathan's is already on the money! What he's saying here is that when you prepare the cell for display, (in the `func tableView(UITableView, cellForRowAt: IndexPath)` data source method), you also set the `listItem` property of the cell to its corresponding Realm object so it knows which one to update. – TiM Sep 27 '16 at 18:56
0

The above answer was helpful in leading me to the actual solution of my issue, but if anyone in the future is curious - I ended up using a closure.

In my custom cell class, I did

@IBAction func stepperValueChanged(sender: UIStepper) {

    selectedListItemLabel.text = Int(sender.value).description
    tapped?(self)

}

And in my view controller, I did

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SelectedListItemsTableViewCell

    cell.stepperControl.value = selectedListItems[indexPath.row].listItemWeight

    // Configure the cell...

    cell.tapped = { (selectedCell) -> Void in

        selectedListItems[indexPath.row].listItemWeight = cell.stepperControl.value

    }

Which allowed me to access each cell's UIStepper in the view controller file.

It helped to read flashadvanced's Option 2 answer in this thread.

Community
  • 1
  • 1
MachTurtle
  • 220
  • 3
  • 10