How to make my own UIPickerView? - ios

Since UIPickerView can't be customized to the level I want (without taking a risk by hiding specific subviews), I need to roll my own to select a time.
I figure I can do this using two UITableViews, but I'm unsure of how to do 2 things:
1) How do I determine which cell is in the middle of the view (i.e. which one did the user select)?
2) How do I make the table snap to the cell nearest to the middle of the view once the user has stopped scrolling?
Thanks for your help.

Why do you say that UIPickerView can't be customized?. It has a bunch of delegate methods made to allow a lot of customization, such as view for rows, height for rows etc etc..
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
Implement the delegate in your ViewController and customize it as you want.

You're just trying to select a time? Then use UIDatePicker. It is a picker view for time/date.

Depending on the type of customisation that you want, you might not need to make your own UIPickerView. You can return your own views for each component using UIPickerViewDelegate. Check pickerView:viewForRow:forComponent:reusingView:

The free Sensible TableView framework has a UITableView based date picker that you could use out of the box. Should save you a lot of time as it handles all the scrolling and table view resizing. Hope this helps.

Related

iOS UI - App Store Explore section Transition

I was wondering if this nice master-detail transition where you click on the tableView cell and it expand to disclose the detail , with the cell's label being the navigation bar title is an interface which is part of the SDK object library or it is a customised one?
This is a custom transition between ViewControllers.
There's a nice example of a few transitions (including this one) in this library.
Of course you'll need to add the tableview etc' but this is a great place to start.
I've tried few things but so far this is the best option I could think of.
Animating the frames of all the visible cells and making use of childViewController is how I achieved it.
Animation test project
https://github.com/armaluca/iOS-App-Store-Explore-Section-Animation
Would be nice to know any other possible solution and ultimately to know how Apple did it!
It is custom implementation.There is no API in UIKit/UITableView which implements this behaviour. Only animation to present a cell is there(which I think is used here).
This behaviour can be implemented like-
Add sections(News, Productivity, etc) in table with zero cells(numberOfRowsInSection: = 0 for all sections). Then on tapping any section just reload that section(reloadSections:withRowAnimation:) by adding a cell to it(numberOfRowsInSection: = 1) and animation(maybe UITableViewRowAnimationMiddle). Scroll that section/row to top in same animation loop(UI update cycle).

iOS Maintaing selected button in Tableview when scrolling

I have a UItableView with custom cell. There are multiple sections, and in those sections multiple rows. In each cell, I have a segment button for "Yes" and "No" selections. I would like to maintain the selections for the segment button when I scroll up and down the table. Can anyone please help. I have looked and did not find anything that helped me. Thank you!
You have multiple ways to do this.
The first one, which I think is the best one, is to use the selected state for the uitableviewcell.
With this method, the table view it self will set the cell's state and according to that state will call didselect or diddeselect on its delegate. Since all is managed by the table view, you don't need additional logic on your cellForRow method, and don't even need to worry about cell reuse.
Another way is to always set the state of your cell's buttons in cellForRow. For this, you need to keep a reference to all the selected rows, and to be able to manage reused cells, you need to make sure its state is always set on cellForRow.
I prefer the first one, but whatever suits you better.

Appropriate way to add multiple UIPicker controls on page

iOS Proficiency: Beginner
If I have a Xib with multiple fields that all need their own Picker View, what's an appropriate/canonical way to add multiple picker views on the page without getting the Design View all cluttered up?
1) Only add the PickerView programmatically and not via the XIB?
2) Use only 1 Picker object and populate it with different values based on the field
selection? (Possible memory benefits?)
3) Place the UIPickers on the View with a tiny height/width and then programmatically adjust height when necessary? Not even sure if the height is adjustable.
4)Combination of some of the above?
You can see in the image below, how cluttered it looks already even with just one picker view:
The view that you have with the text fields and picker views would lend itself to be part of a UITableView.
See the calendar app when you add an event.
You can set this up fairly easily by using a static UITableView.
I'm replying on my phone at the moment but will look for a tutorial if you would like.
If only one pickerView will be visible at once, then you should consider using only one pickerView and configure it's delegate/datasource so that you can give it a different datasource for each field.
Unless more than one is visible at once, there really isn't any reason to use more than one in your nib file. And even if you used more than one, you would still have to configure you delegate/datasource methods to handle each individual picker.
Hope that helps.
EDIT: It would be a little bit of work, but if you wanted the pickerView to animate in and out of the view whenever you need and if you wanted to clean your Xib up even more you could do the following:
Create a subview containing your pickerView
Set up a protocol on the subview to allow you to pass the selected value back to the view controller.
Set up your viewController to conform to the protocol on your picker subview.
Set the pickerView to be each textField's inputView.
Set the textField's delegate methods to configure the dataSource of your subview when editing begins.
By doing this, you have set your textField so that when it receives firstResponder that it will display the pickerView instead of a keyboard.

how to connect textfield with table view of listed options instead of keypad

I need to be able to disable the keypad in a textfield, and instead when the user taps on it to start editing, a new table view will appear presenting a list of possible strings with which the textfield should be filled.
Anyone has any suggestions on how to achieve this?
Thanks in advance
p.s. I tried already but this functionality cannot be nicely implemented with a picker in my case, as there are too many options to choose from and (more importantly) each one of them is a rather long string and cannot appear entirely in a picker.
I believe you just need a regular cell that, when tapped, pushes a new detail UITableViewController with all the options to choose from. Once an option is chosen, set the cell's textLabel to whatever option have been selected.
If you'd like to go for an easier path, then you should also probably check the free Sensible TableView framework, as it has these kinds of cells out of the box (called selection cells). You just pass on your strings array to the selection cell and it will automatically display the detail view, and even assign the selected option to one of your object's properties if you wish. Should save you some good amount of manual work. Good luck!

Difference between Pickerview and Tableview in iOS

I am new to iOS Developement, Can any one say what is the major difference between this two views, Table view and Picker View..
A table view is much more versatile than a picker. It is the main mechanism to display list data on mobile devices. The most important difference is that you can subclass the main unit, UITableViewCell to create rich and individual interfaces. Conceptionally, tableviews can be basically infinite in content length.
A picker view is much more limited both in size, styling and features. It is really meant as a standardized way to choose from a limited amount mainly strings.
I do not think that there is requirement of compare UITableViewController and Pickerview controller. We can customize UITabelview as per requirement by using custom cell but not in picker view.
Picker view is just to choose one from the list. We can use table view in place of picker as per need but not picker in place of table view. If we take technically there is not much difference between in delegate methods calling on events. But On interface builder and as per utilization of view components table view have more supervision.

Resources