Cody Rogers
Cody Rogers

Reputation: 103

Swift/iOS equivalent of spinner in Android?

I am trying to design a screen for an iOS app where a tableView cell will have a dropdown which expands onClick and contains options from some data source. Here is an image:

enter image description here

The Serving tableview cell is where I'm trying to add this drop down.

I'm a fairly new iOS developer, but I know in Android this would be accomplished with a Spinner. I looked into picker views but I am unsure how to insert it to appear onClick.

The tableview displayed has been implemented as a 2 section tableview. I wanted to do the top section as a static tableview but there doesn't seem to be a way to implement a static and dynamic tableview in the same viewcontroller.

Any advice on how to achieve this functionality or more information?

Upvotes: 4

Views: 7550

Answers (1)

user2437424
user2437424

Reputation: 75

A UIPickerView is the object you're looking for. To insert it into your interface on a click of the cell, you can implement the UITableView delegate method tableView:didSelectRowAtIndexPath: , which is called every time a table view row is selected:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == ROW_OF_CELL { //ROW_OF_CELL is index of the cell you just tapped  
    //... code to display picker view here ...
    }
}

And after you select your item, you can remove it and update your model using the UIPickerView delegate method pickerView:didSelectRow:inComponent:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    //... code to update your model and remove picker view here ...
}

There are plenty of tutorials about using UIPickerViews if you just google them, one (of many) examples here: http://codewithchris.com/uipickerview-example/

Upvotes: 2

Related Questions