UIPickerView Delegate and Data Source template


/ Published in: Objective C
Save to your folder(s)

This is a template for a UIPickerView delegate and data source. Implement these methods to manage UIPickerViews.


Copy this code and paste it in your HTML
  1. #pragma mark -
  2. #pragma Picker View Data Source methods
  3.  
  4. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  5. {
  6. //Return the number of components
  7. return 1;
  8. }
  9.  
  10. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  11. {
  12. //Return the number of rows in the component
  13. return 1;
  14. }
  15.  
  16. #pragma mark -
  17. #pragma Picker View Delegate methods
  18.  
  19. //Set the dimensions of the picker view.
  20. /*
  21. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  22. {
  23.   return 40.0f;
  24. }
  25. */
  26.  
  27. /*
  28. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
  29. {
  30.   return 200.f;
  31. }
  32. */
  33.  
  34. //The methods in this group are marked @optional. However, to use a picker view, you must implement either the pickerView:titleForRow:forComponent: or the pickerView:viewForRow:forComponent:reusingView: method to provide the content of component rows.
  35. //If both pickerView:titleForRow:forComponent: and pickerView:attributedTitleForRow:forComponent: are implemented, attributed title is favored.
  36.  
  37. // Return a string representing the title for the given row.
  38. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  39. {
  40. //return [dataSource objectAtIndex:row];
  41. return @"Nil";
  42. }
  43.  
  44.  
  45. //Return a view to use for the given row:.
  46. //reusingView is A view object that was previously used for this row, but is now hidden and cached by the picker view.
  47. //If the previously used view is good enough, return that.
  48. //The picker view centers the returned view in the rect for the given row.
  49. /*
  50. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  51. {
  52.   if (view != nil)
  53.   {
  54.   return view;
  55.   }
  56.   else
  57.   {
  58.   UIView* view;
  59.   view.backgroundColor = [UIColor whiteColor];
  60.   return view;
  61.   }
  62. }
  63. */
  64.  
  65. //Return a styled sring representing the title for the given row.
  66. /*
  67. - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
  68. {
  69.   //return [dataSource objectAtIndex:row];
  70.   return (NSAttributedString*)@"Nil";
  71. }
  72. */
  73.  
  74. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  75. {
  76. //Do something when the row is selected.
  77. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.