Setting UIPickerView from NSArray Object - ios

I am trying to set the UIPicker View with the date for the last 20 Mondays. The array is filled with 20 Week Objects. e.g NSDate *weekDate=3/24/2014 NSString *weekDateString=Week of 3/24/2014.
When I set the rows initially it shows the first 5-6 rows correctly, when I try to scroll the picker is trying to set the title for the new rows it is displaying, but setTitle is logging (null) for the weekDateString. The list is still showing a count of 20, when I log the count in titleForRow.
If I set the NSArray just NSString there are no issues.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_weeks = [Week getWeeks];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=#"Weeks";
[self.navigationController.navigationBar.layer insertSublayer:[LayoutColors getNavBarGradient:self.navigationController.navigationBar.bounds] atIndex:1];
self.navigationController.navigationBar.titleTextAttributes
= #{NSForegroundColorAttributeName : [UIColor whiteColor]};
int startYValue = self.navigationController.navigationBar.frame.size.height + HeaderHeight;
int width = self.view.frame.size.width;
_picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, startYValue, width/2, DATE_PICKER_HEIGHT)];
_picker.delegate = self;
_picker.dataSource = self;
[self.view addSubview:self.picker];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return _weeks.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
Week *week = [_weeks objectAtIndex:row];
return week.weekDateString;
}

There is nothing wrong with your code, so one is compelled to infer that there is probably something wrong with your model object, i.e. your _weeks array is not in fact correctly populated the way you think it is - the weekDateString is apparently missing for some of the Week objects. You should probably go back and look at how you are generating this array.

Related

iOS:Subclassing UIPickerView not loading in the main view

I'm trying to subclass UIPickerView but is not loading in the main view.
Here is my code:
//Picker.h:
#interface Picker : UIPickerView
- (id)initWithFrame:(CGRect)frame;
//Picker.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"bla";
}
This the code on my viewController:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
self.myPicker = [[Picker alloc] initWithFrame:self.view.frame];
[self.view addSubview:_myPicker];
}
Any of you knows why the picker is not loading on my view?
I'll really appreciate your help.
First, add <UIPickerViewDelegate, UIPickerViewDataSource> in header of Picker.h.
Then, assign delegate to your own Picker object.
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.delegate = self; // <-- add this line
}
return self;
}

User defined picker view to display 12 digits in it

I want to use picker view to display 12 digits in it that digit comes dynamic and it has to be light flight schedule display . Digits will be increasing always that i want to make it through picker. As i am new to IOS need your help. Thank you
#import "DetailViewController.h"
#interface DetailViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerview = [[UIPickerView alloc]init];
pickerview.delegate = self;
pickerview.dataSource = self;
[self.view addSubview:pickerview];
}
- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 12;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSNumber *digit = #(row);
return [digit stringValue];
}
#end
You can use Simple Library and Then Add array of Digits .
here are Few Simple Library , try it once.
CPPickerView
ActionSheetPicker-3.0
AKPickerView

UISearchControl delegate fired but no objects showing up in Tableview

I've implemented UISearchControl (iOS8 version which is slightly different then previous ones). It seems to be firing correctly when I type stuff in according to the NSLog statement seen below, only issue is it the UITableView actually never gets updated, and I added a NSLog to the } else { part of the numberOfRowsInSection to confirm that it's not called like so:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return self.objects.count;
} else {
// NOT BEING FIRED WHEN STUFF IS TYPED IN SEARCH BAR?
NSLog(#"Search Results Returned in TableView");
return self.searchResults.count;
}
}
Although these methods are firing fine...and updating the searchResults array correctly according to the NSLog added in the updateFilter method:
#pragma mark - UISearchResultsUpdating
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
[self updateFilteredContentForSaleName:searchString];
// ASSUMING THIS IS WHAT CAUSES TABLEVIEW TO REFRESH, BUT ISNT REFRESHING IT WITH NEW ARRAY?
[((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
}
#pragma mark - Content Filtering
- (void)updateFilteredContentForSaleName:(NSString *)saleName {
[self.searchResults removeAllObjects];
for (PFObject *sale in self.objects)
{
NSString *saleTitle = [sale objectForKey:#"name"];
if ([[saleTitle lowercaseString] hasPrefix:[saleName lowercaseString]])
{
[self.searchResults addObject:sale];
}
}
// THIS LOG SHOWS SEARCH RESULTS CORRECTLY HOW THEY SHOULD BE, JUST NO UPDATE TO UITABLEVIEW
NSLog(#"%#", self.searchResults);
}
But simply no update to the tableview...
Any idea why this is? Am I calling the refresh wrong here?:
[((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
I've tried PFQueryTableViewController * too since that's essentially what I'm using for actual view controller, but no luck.
EDIT
Here is the code used to create the UISearchControl
#interface LocalSalesViewController () <UISearchResultsUpdating>
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) NSMutableArray *searchResults; // Filtered search results
#end
#implementation LocalSalesViewController
- (id)initWithCoder:(NSCoder *)aCoder {
self = [super initWithCoder:aCoder];
if (self) {
self.parseClassName = #"Sales";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 25;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.searchResults = [NSMutableArray array];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
}

Reload data for UIPickerView from another ViewController

My problem is about set new data to my UIPickerView from another ViewController. I have main.storyboard(ViewController.h and ViewController.m), I have a pickerview(I call it aPicker) in it. I have a CustomTableViewController to load some images. When I tap on these images, data of aPicker will change.
aPicker in ViewController.m
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView;{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(pickerView== aPicker)
{
//I will code it later
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
if(pickerView==aPicker)
{
return [aPickerAdapter count];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView* tmpView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 32)];
UILabel *lbl;
if(pickerView==aPicker)
{
lbl = [[UILabel alloc] initWithFrame:CGRectMake(64, 0, 256, 32)];
[lbl setText:[aPickerAdapter objectAtIndex:row]];
}
[tmpView insertSubview:lbl atIndex:0];
return tmpView;
}
I call custom table here:
NSMutableArray* tranferData= [[NSMutableArray alloc] init];
[tranferData addObject:aPickerAdapter];
[tranferData addObject:aPicker];
[tranferData addObject:self];
aPicker.delegate=self;
[tranferData addObject:aPicker.delegate];
aPicker.dataSource=self;
[tranferData addObject:aPicker.dataSource];
customTableViewController=[[CustomTableViewController alloc] initWithNibName:#"CustomTableViewController" bundle:[NSBundle mainBundle] set:tranferData];
customTableViewController.delegate=self;
[self.view addSubview:customTableViewController.view];
Receive in CustomTableViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(NSMutableArray *)myarray{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
myArray=myarray;
// Custom initialization on passed parameter observation object
aPickerAdapter=[myarray objectAtIndex:0];
aPicker=[myarray objectAtIndex:1];
tranferSeft=[myarray objectAtIndex:2];
pickerDelegate=[myarray objectAtIndex:3];
pickerDataSource=[myarray objectAtIndex:4];
}
return self;
}
When tap image in table
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string= [#"tap to row " stringByAppendingString:[NSString stringWithFormat:#"%d",indexPath.row]];
NSLog(string);
//NSString *str= [onlineIdList objectAtIndex:indexPath.row];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Demo" message:str delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
//[alert show];
NSMutableArray *testArr= [[NSMutableArray alloc]init];
[testArr addObject:#"2"];
[testArr addObject:#"4"];
[testArr addObject:#"6"];
//aPicker.delegate= pickerDelegate;
//aPicker.dataSource= pickerDataSource;
**aPickerAdapter=testArr;
[aPicker reloadAllComponents];**
[self hideAndShowSubChannel];//this funtion is to hide and show aPicker, defaut aPicker is hide, on first tap it will show([aPicker setHidden:NO]), and it hide on second tap....
}
When I tap to image, picker only hide and show(I think it mean my program can understand the pointer point to aPicker),but it can't add data from my testArr to aPicker, I also try to reset delegate and datasource received, it till not works. I test with the same code(create testArr, set aPickerAdapter, reloadAllComponent... )in ViewController.m, it works. Why I can't reload it? Help me please!
ps: I'm just a newbie and not good at English. If I have any mistake, please forgive me, thanks
I would do it differently. You are trying to have your TableViewController update another view controller's view directly - you shouldn't. You need instead to pass the data back to your first view controller, so that it can update its own views. There are various techniques for this (see this link), but in your circumstances I would implement a method in your view controller and call it from your custom TableViewController:
In ViewController:
-(void)updatePicker:(NSArray *)newArray {
aPickerAdapter = testArr;
[aPicker reloadAllComponents]
// and do anything else you need
}
In your CustomerTableViewController didSelectRow method:
[self.delegate updatePicker:testArr];
I've solved it by the way of #pbasdf anh his link
(answer of #Matt Price really useful). Tks all :D
Try implementing the UIPickerViewDelegate-Method
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
instead of the viewForRow-method.
For example like this:
- (NSString *)pickerview:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [aPickerAdapter objectAtIndex:row];
}

iPhone UIPickerView not loading values

I attempted to build a basic application prototype (mimic real application flow). I created one view controller class and linked the UIPickerView to the .h file. Wrote the logic but still nothing is reflecting in the picker.
Am I missing something? Here you go with the code:
#implementation SelectCountryForViewAdsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1; // For one column
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.colorArray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.colorArray objectAtIndex:row]; // If it's a string
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.colorArray = [[NSArray alloc] initWithObjects:#"Blue",#"Green",#"Orange",#"Purple",#"Red",#"Yellow" , nil];
}
and in the .h file:
#interface SelectCountryForViewAdsViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
#property (strong, nonatomic) IBOutlet UIPickerView *countrySelector;
#property (strong, nonatomic) NSArray *colorArray;
#end
Any help is appreciated. I am a beginner in iOS devs.
Please check below image you need to right click on UIPicker view and connect it File owner.
Please check below link for more detail
http://www.techotopia.com/index.php/An_iOS_5_iPhone_UIPickerView_Example
http://iosmadesimple.blogspot.in/2012/09/uipickerview-tutorial.html (More helpful)
http://www.developersalmanac.com/uipickerview-tutorial/
[countrySelector setDelegate:self];
[countrySelector setDataSource:self];
//Use this code in Viewdidload method
Add UIPickerViewDataSource and UIPickerViewDelegate in your interface file.
In viewDidLoad:
self.countrySelector.delegate = self;
self.countrySelector.dataSource = self;
You can set the delegate and dataSource directly from the xib file. But don't forget to mention them in the interface file.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
[lblRow setTextAlignment:NSTextAlignmentCenter];
[lblRow setTextColor: [UIColor strawberryColor]];
[lblRow setText:self.colorArray[row]];
[lblRow setFont:[UIFont fontWithName:#"Avenir-Medium" size:18]];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
}

Resources