How can pickerview have date component to it?
I need to add a date component to my PickerView. Any ideas on how to get the date component? One way is to manually maintain all the date related info, take current date add and subtract to get the reqd dates, but this is very tedious, any other methods?
EDIT:I have not yet got about to writing the code, i need some ideas on how to go about getting the values for the date component(not UI). One solution I have 开发者_JAVA百科is to take the current date and then use the 'dateByAddingTimeInterval' to maintain the the last 50 and next 50 days, but his approach is very crude. Is there a better way to do this?
Going from comments here:
I suggest you to try this one approach.
- Creating one
UIPickerView
. - Setting its parameters (step 1 in class that implements
UIPickerViewDataSource
):- Implement
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
that will provide number of components. In your case it is 3 :return 3;
- Implement
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
that will return the number of row for each component.
- Implement
- Setting its parameters (step 1 in class that implements
UIPickerViewDelegate
):- Implement
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
which will returnUILabel
for component 0 and 1 andUIView
with twoUILabel
's as subview (gray one and black one) for component 2
- Implement
Also you can make more customization by implementing other methods in delegate class:
Setting the Dimensions of the Picker View
– pickerView:rowHeightForComponent:
– pickerView:widthForComponent:
Setting the Content of Component Rows
– pickerView:titleForRow:forComponent:
Complete list of methods you can find here: UIPickerViewDelegate Protocol Reference
I went ahead and used the same approach that i had mentioned in the question(given below). Even though its crude, it gets the work done. I have not tested this approach for large values of date in the third component. As of now i just have (30+30) values including the current date and its pretty stable and quick.
"One solution I have is to take the current date and then use the 'dateByAddingTimeInterval' to maintain the the last 50 and next 50 days, but his approach is very crude."
精彩评论