Objective C how to re add the deleted items in NSMutableArray - ios

In my code, three text fields are there for picker views. For example in the first text field, the user selects A+ blood group, in the second text field we don't want to display A+ blood group. Remaining items need to display. Text field 3 also same. For example, in text field 2 the user selects B+ blood group, in text field 3 B+ and A+ blood group needs to remove in array list. For example the user again selects the first text field and reselects another blood group like A- ve group re add the A+ blood group in array list. The same thing in text field2 and text field3. I already completed the one task, i.e removing items from array. The pending task is re adding the items in array.
- (void)viewDidLoad {
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc] initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if (!isBloodGroupFieldSelected) {
return 1;
}
else if (!isGenderGroupFieldSelected) {
return 1;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isGenderGroupFieldSelected) {
return [dataArray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
return dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
return dataArray[row];
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
txtField1.text = dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1) {
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
- (void)done:(id)sender {
[myPickerView selectRow:0 inComponent:0 animated:YES]; // On 27/04/2020
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}

Related

objective c how to remove the item in second and third text field based on select the first text field item

I have one UIPickerview. I have a json response. I have a three text fields. If user select the first text field display picker view then user select the particular item. And second and third text field also same. My requirement Is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same.
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2){
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
-(void)done:(id)sender
{
[myPickerView selectRow:0 inComponent:0 animated:YES]; // On 27/04/2020
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}

Objective C how to remove items from array based on other arrays items

I have three UIPickerviews. I have a json response. I have a three text fields. If user select first picker view the related data is displaying in first textfields. And second and third picker view also same. My requirement Is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same.
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
You have most of the code you need already. The pickerView:didSelectRow:inComponent: delegate method will be called when any of your pickers have been changed, assuming they each have their delegates set to the controller above. Although, I only see one picker view defined in the code above. Let's assume your picker views are called myPickerView, myPickerView2, and myPickerView3. The code would roughly be:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (pickerView == pickerView1) {
// the first picker view lseection has changed
// update myPickerView2 and myPickerView3 to reflect
// the values you want based on this selection
} else if (pickerView == pickerView2) {
// update the other views based on this selection
} else ...
....
}

Add Static Row To UIPickerView

In my app, I query Parse.com to get a list of groups to populate a UIPickerView. I want to add one more option to the UIPickerView at the very top that is called "Create New Group", in which it will pull up a UITextField to make a new group and move from there. How do I go about adding this to all the other fields? Here is how I have it created so far.
-(void)addPickerView{
__block NSMutableArray *arr = [[NSMutableArray alloc] init];
PFQuery *rejectedNumber = [PFQuery queryWithClassName:#"Group"];
[rejectedNumber orderByAscending:#"GroupName"];
[rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!objects) {
// Did not find any UserStats for the current user
NSLog(#"NotFound");
} else {
pickerArray = objects;
[myPickerView reloadAllComponents];
}
}];
//pickerArray = GoGo;
self.theView.signUpView.additionalField.delegate = self;
[self.theView.signUpView.additionalField setPlaceholder:#"Choose Group"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(finishIt)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
self.theView.signUpView.additionalField.inputView = myPickerView;
self.theView.signUpView.additionalField.inputAccessoryView = toolBar;
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[self.theView.signUpView.additionalField setText:self.theGroup];
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
PFObject *object = pickerArray[row];
//Assuming "Category" is a string here for your title
self.theGroup = object[#"GroupName"];
return object[#"GroupName"];
}
You can add achieve that in two ways:
Add an extra object at the first index of your array for showing "Create New Group"
You can use tweak the datasource and delegate methods of UIPickerView. Like:
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerArray count] + 1; // +1 for the additional row
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
if (row == 0)
{
// Create New Group clicked
}
else
{
// Some other option selected
}
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *groupName = "Create New Group";
if (row != 0)
{
PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
self.theGroup = object[#"GroupName"];
groupName = object[#"GroupName"];
}
return groupName;
}

Bar button methods in toolbar are not calling

I have created a bar button items programatically. But the respective methods of cancel and done are not firing.
I am adding the picker view as input view for textfield programatically. On top of picker view i placed a toolbar with done and cancel buttons. Toolbar is the subview of pickerview.
Here is my code :
(void)awakeFromNib
{
contactAgeRange = [[NSMutableArray alloc]init];
for (int i = 18; i <= 100; i++)
{
[contactAgeRange addObject:[NSNumber numberWithInt:i]];
}
personGender = [[NSMutableArray alloc]initWithObjects:#"Male", #"Female", nil];
[self initializePickerView];
self.cellTextField.userInteractionEnabled = NO;
self.cellTextField.delegate = self;
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:#selector(doneButtonPressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:#selector(cancelButtonPressed)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, space, doneButton, nil]];
[self.pickerView addSubview:toolBar];
}
-(void) configureCell
{
switch (self.cellType) {
case ContactCellTypeAge:
self.cellTextField.inputView = self.pickerView;
break;
case ContactCellTypeGender:
self.cellTextField.inputView = self.pickerView;
break;
}
}
-(void)initializePickerView
{
self.pickerView = [[UIPickerView alloc]init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (self.cellType)
{
case ContactCellTypeAge:
return [contactAgeRange count];
break;
case ContactCellTypeGender:
return [personGender count];
break;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
NSString *age = [[contactAgeRange objectAtIndex:row] stringValue];
return age;
}
else if (self.cellType == ContactCellTypeGender)
{
NSString *gender = [personGender objectAtIndex:row];
return gender;
}
return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
pickerRowText = [[contactAgeRange objectAtIndex:row] stringValue];
//self.cellTextField.text = pickerRowText;
}
else if (self.cellType == ContactCellTypeGender)
{
pickerRowText = [personGender objectAtIndex:row];
//self.cellTextField.text = pickerRowText;
//[self.cellTextField endEditing:YES];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.setNextResponderActionBlock)
{
self.setNextResponderActionBlock();
}
return YES;
}
-(void) cancelButtonPressed
{
[self.cellTextField resignFirstResponder];
}
-(void) doneButtonPressed : (id)sender
{
if (self.cellType == ContactCellTypeAge)
{
self.cellTextField.text = pickerRowText;
}
if (self.cellType == ContactCellTypeGender)
{
self.cellTextField.text = pickerRowText;
}
}
Please help me. Thanks in advance
configureCell in this Method add self.cellTextField.inputAccessoryView = toolbar;

Insert element at NSMutableArray's end

I am loading an array from a web service and displaying them in picker and all works fine. My array contains random number of questions for every category of questions. Now what i want is that i wanna add one more entry "Your own question" at the end of array so that the last row of picker displays "Your own question". How can i do this
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
retval.text = [quesArray objectAtIndex:row];
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}
This code only displays number of rows(questions) fetched from the web service.
EDIT 1:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count];
}
}
EDIT 2:
Resolved by making another NSMutableArray with my custom object "Your own question" and then appending it with the array fetched from web service
NSMutableArray *customObj = [NSMutableArray arrayWithObject: #"test"];
[quesArray addObjectsFromArray:customObj];
In UIPickerViewDataSource's method called pickerView:numberOfRowsInComponent: return your array's count plus one value(return arr.count+1;), and in the method of UIPickerVIewDelegatethat you have implemented return your label with the text when the component is greater than arr.count-1 (if(row>(arr-1)){//return the label with 'Your own question'}). Good Luck!
EDIT:
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count]+1;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
if(row>quesArray.count-1){
retval.text = #"You own question";
}
else{
retval.text = [quesArray objectAtIndex:row];
}
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}

Resources