I have a UIPickerView inside a custom tableViewCell (subclassed). I'am able to populate it and get data back from it. More or less.
I have this method I implement in order to get info everytime some of the components changed:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (self.pickerDelegate !=nil &&[self.pickerDelegate conformsToProtocol:#protocol(PickerCellDelegate)]) {
if ([self.pickerDelegate respondsToSelector:#selector(somethingOnThePickerIsSelected:selectionArray:)]){
NSMutableArray *pepe;
for (int i=0; i<[[self.cellPickerInputDictionary objectForKey:#"components"] count]; i++) {
NSObject*foo=[[[self.cellPickerInputDictionary objectForKey:#"components"] objectAtIndex:i] objectAtIndex:[self.cellPicker selectedRowInComponent:i]];
[pepe addObject:foo];
NSLog (#"foo: %#", foo);
}
NSLog (#"pepe: %#", pepe);
[self.pickerDelegate somethingOnThePickerIsSelected:self selectionArray:pepe];
}
}
}
I have two components, but in order to make it "universal" (independent of a particular situation) I don't want to hard-write numbers here and there.
In the example shown, I don't understand why the NSLog shows correct for the variable foo but shows null for the NSMutableArray pepe.
Any ideas? Thanks in advance.
You are not allocating your mutable array pepe.
NSMutableArray *pepe=[[NSMutableArray alloc]init];
In fact, you can do that on your init method, and declare pepe as property
Related
This question already has answers here:
Removing object from NSMutableArray
(5 answers)
Closed 7 years ago.
My program has a NSMutable Array named as "matchedCards", and I have added few object in it of type Card, now I need to remove the objects from the array, and I use the following code for it:
for (Card * removeCards in matchedCards)
{
[self.matchedCards removeObject:removeCards];
}
The first card-object gets removed , and after that the program gets crashed , Can anyone explain the reason behind it, if it successfully removes the first object, why it starts throwing error from 2nd object onwards
You can't remove elements from an array while fast-enumerating it.
If you simply want to remove all objects do
[self.matchedCards removeAllObjects];
If you want to remove only certain elements however, remember their indices in an IndexSet and remove those
NSMutableIndexSet* indexesToRemove = [NSMutableIndexSet new];
for (NSUInteger index = 0; index < [self.matchedCards count]; ++index) {
if (whatever) {
[indexesToRemove addObject:index];
}
}
[self.matchedCards removeObjectsAtIndexes:indexesToRemove];
You can't remove an object from an array while iterating on it. Do this instead:
for (Card * removeCards in [matchedCards copy])
{
[self.matchedCards removeObject:removeCards];
}
Read the crash log. It will say something along the lines of...
"Collection was stated while being enumerated"
Or something like that.
You can't mutate an array while iterating over it using a for:in loop.
You can do this though...
[matchedCards enumerateObjectsUsingBlock:^(Card *removedCards, NSInteger idx, BOOL *stop) {
[self.matchedCards removeObject:card];
}];
Also, with your current code you are actually removing all of the objects from the matchedCards array. It will result in an empty array. Are you sure that's what you want?
The reason is because you are removing the current object and ruin the for-statement
Here's a solution:
for (int i = 0; i < self.matchedCards.count; i++)
{
if ([self.matchedCards[i] isKindOfClass:[YourClass class]])
{
[self.matchedCards removeObject:self.matchedCards[i]];
i--; // invalidate the removed index
}
}
NSLog(#"%#", self.matchedCards);
Take note that i-- is important, else you will not get through to the last element of the array..
Hope this helps you.. Cheers..
// if remove all objects
[matchedCards removeAllObjects];
// if you want to remove using index
for (int i =[matchedCards count]-1; i>=0; i++) {
if (condition) {
[matchedCards removeObjectAtIndex:i];
}
}
I am working on a picker view with 3 columns. I want to determine the 3rd column value from the second column. Here is part of my code
- (void)viewDidLoad {
[super viewDidLoad];
Number=#[#"Trans",#"1st",#"2nd",#"3rd",#"4th",#"5th",#"6th",#"7th",#"8th",#"9th",#"10th",#"11th",#"12th"];
Season=#[#"Spring",#"Summer",#"Fall"];
Course=#[#"CHEM1100 General Chem I",#"CHEM2100 General Chem II",#"CHEM3511 Org Chem",#"CHEM3521 Org Chem II"];
// Course=#[#"Summer1",#"Summer2",#"Summer3"];
Course = [Course sortedArrayUsingSelector:#selector(compare:)];
Number =[Number sortedArrayUsingSelector:#selector(compare:)];
Season =[Season sortedArrayUsingSelector:#selector(compare:)];
}
Here is my question, how can I achieve the goal like
if (Season==#"Spring")
Course= #[#"CHEM1100 General Chem I",#"CHEM2100 General Chem II",#"CHEM3511 Org Chem",#"CHEM3521 Org Chem II"];
else if (Season==#"Summer")
Course=#[#"Summer1",#"Summer2",#"Summer3"];
Sorry, I don't know what method to use to complete the code. Any idea please?
To show the column value, I use the following code:
- (IBAction)addCourse:(UIButton *)sender {
NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];
NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];
NSString *msg=[[NSString alloc ]initWithFormat:#"%# ",course];
_courseLabel.text=[msg substringToIndex:8];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==kNumComponent)
return [Number count];
else if(component==kSeaComponent)
return [Season count];
else return [Course count];
}
According to the documentation, you must provide your UIPickerView with a datasource and a delegate. The datasource tells the view how many components and rows there are, while the delegate tells it the content of the components row. Let us suppose that you implement titleForRow: in your delegate. Since this is going to change based on a selection, you will need to do two things: (i) make sure your delegate object knows which selection was made (e.g. #"Spring") and (ii) you will then need to call the UIPickerView reloadComponent: method so that your delegate's titleForRow: method will be called.
Example: suppose your action method is componentSelected:
-(void)componentSelected:(id)sender
{
NSInteger row = [myPicker selectedRowInComponent:seasonComponent];
myDelegate.season = row;
[myPicker reloadComponent:courseComponent];
}
By the way, it is worth getting into the habit of being very careful with "=", "==", isEqualToString: etc. to avoid bugs. Your question has several basic syntactical errors. The line
if(season = #"spring")
should be
if([season isEqualToString:#"spring"]);
not just because "=" is assignment, but because "==" makes no sense if one of the arguments is a literal (it is essentially pointer comparison). isEqualToString will compare the target string with the argument string.
Sorry to kind of be vague in the question. I currently have a pickerView populated by an array of strings. The string is set by this function...
_homePlayer = _homePlayersArray[indexPath.row];
// add to the copy
[_homeConfirmedPlayersArray addObject:[NSString stringWithFormat:#"%d %# %#",_homePlayer.number,_homePlayer.firstName,_homePlayer.lastName]];
I have it populate the pickerView which works properly. I now want to be able to pick from the picker and take just the first part of the string and set it as an NSString. Is this possible and if so how would i go about doing this? Could i change some things to be able to accomplish this? thanks in advance!
In the delegate for your UIPickerView
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
Player *player = _homePlayersArray[row];
NSInteger number = player.number;
}
Currently I have a custom view table cell and a text field just above it. I want to get the text from the UItextfield and put that into an NSMutableArray.
Pseudocode:
String text = _textfield.text;
[array addObject:text];
NSLog{array}
In my header file I have created the textfield and the array.
I currently receive the error : 'CustomTableView:[340:11303] array: (null)' when I NSLog.
I am not to sure why the text from the textfield is not getting added to the array. If any one is able to help it will be greatly appreciated.
Note - My textfield is above the custom cell not in it. I have even tried just adding a string to the array directly and logging it and I get the same error. So I would assume that this is something to do with the array.
did you initialize your Array.take a MutableArray and initialize it.
NSMutableArray *array=[NSMutableArray alloc]init];
You mentioned that you have declared the textfield and the array in your header file...
Have you initialised the variable array?
e.g.
array = [NSMutableArray new];
It looks like you are not actually creating the array. In Objective C, you do not create things in header file, you declare them. The implementation files(.m files) do all the work.
Try this:
NSString *text = _textfield.text;
array = #[text]
NSLog( #"%#", array );
This is how you should print your array,
NSLog(#"%#", array);
It looks as if your a newbie to ios.Go through the objective-c and Read the apple documentation carefully.
NSString * text = self.textfield.text;
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:text];
NSLog(#"%#",array);
For me this is what worked...
I have taken one textfield inside tableviewcell. I am creating textfields based on dynamic data. My requirement is , I need to get textfields text which are created dynamically.
For getting text in another method
NSIndexPath *indexPath = [tableViewObj indexPathForCell:customCell];
if (indexPath.row==0)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==1)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==2)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
Like this I have added textfield text to array. Let me know if you have any doubts.
Anyone knows how to make continuous values in a component of a custom picker, like months wheel in date UIPickerView? Here is my source array data:
self.one =[[NSMutableArray alloc]init];
for (int i=0; i<=101; i++) {
[one addObject:[NSNumber numberWithInt:i]];
}
//and here my titleforrow method
if (component==0) {
return [[one objectAtIndex:row] stringValue];
}
Take a look at DialController here.
There's a video and some example code.
You might be interested in this DLPickerView. This picker view can totally replace UIPickerView. And yes, you can make DLPickerView scroll cyclically. And config many other new features that UIPickerView doesn't have.