Niko Niko - 14 days ago 5
iOS Question

Swift XCode - Changes in storyboard aren't being reflected in the iPhone simulator

I'm trying to make a simple UITableView that has a button appended to the right side of every cell. I added the button to the prototype cell in my storyboard and set up the constraints so that AutoLayout takes over and puts it in the right spot. When I go to preview the layout in the Assistant Editor, everything looks great. However, once I start the simulator and go to that view, the button is no longer there (the cell is blank).

I ran into a similar problem when I tried to add a Disclosure Indicator and it wasn't showing up - to fix it I had to add that accessory programatically. Isn't the point of storyboard to help make changes without using code? I'm still really new at this and would appreciate some guidance.

EDIT: Some code as requested (sorry):

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()

let addButton = UIButton()

let menuItem = currentOrder?.getSortedMenuItems()[indexPath.row]
let itemPrice = "\(currentOrder!.menu[menuItem!]!)"

cell.textLabel!.text = menuItem! + " - " + itemPrice
return cell
}


Most of the code in there is pretty irrelevant, but as you can see I tried adding a button and then stopped cause I wasn't sure where to go from there. I was under the impression that all of this could be done using storyboard.

Answer

The problem which you have arises from the fact that you are NOT using the cells which you created in your storyboard. When you call

let cell = UITableViewCell()

it creates a cell with default style. What you want to do instead is to use the cells which you created in the storyboard.

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

// setup your cells (e.g. update its title)

return cell

}

You should change the "cellWithSwitch" to the identifier which you have set for your cell in storyboard.

You can find the identifier of your cell by selecting it in the storyboard and then checking info in the Attributes inspector:

enter image description here

If you haven't yet set this identifier - set it and then use it. And then changes which you make in storyboard will get reflected on the phone ;)

Extra

It is often necessary to subclass your cell to make it easier to work with. Here is an example:

class MyCell:UITableViewCell {

    @IBOutlet var myButton: UIButton!

}

Now, you have to do 3 other things:

1) Select your cell in the storyboard and set its class to MyCell in Identity inspector (that's the third tab in the image which I posted)

2) Open Connections inspector (last tab) and connect the myButton outlet

3) Modify your cellForRowAtIndexPathFunction:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellWithSwitch") as! MyCell

// setup your cells (e.g. update its title)

// you can now access your button as cell.myButton

return cell
}
Comments