There is a UIPickerView standard, 1 piece. It is required to create a UIPickerView with pictures and UILabel , into which I will display different data in my font. What are some ideas?
- Everything turned out to be very simple, as I did, and I will tell you a little later when I’ll fix everything and fix bugs! - Red Rocket
1 answer
In the .h file we create our Picker, since we will release it, then we make it global UIPickerView *ourPicker; while on the .h file you can save and go to the .m file. Here we initialize our picker:
ourPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 215.0f, 320.0f, 100.0f)]; ourPicker.delegate = self; ourPicker.showsSelectionIndicator = YES; [self.view addSubview:ourPicker]; We start and see an empty piker :) We create an array in which images for the picker NSMutableArray *imageArray; will be stored NSMutableArray *imageArray; In the m file, initialize it and fill it with pictures.
imageArray = [[NSMutableArray alloc] init]; [imageArray addObject:[NSString stringWithFormat:@"img1"]]; [imageArray addObject:[NSString stringWithFormat:@"img2"]]; [imageArray addObject:[NSString stringWithFormat:@"img3"]]; [imageArray addObject:[NSString stringWithFormat:@"img4"]]; [imageArray addObject:[NSString stringWithFormat:@"img5"]]; [imageArray addObject:[NSString stringWithFormat:@"img6"]]; And so, the array is created and filled with pictures, I do not specify the format of pictures, I will indicate later. Then we need an array to display the information in our tags, do the same:
arrayOfLabels = [[NSMutableArray alloc] init]; [arrayOfLabels addObject:@"description1"]; [arrayOfLabels addObject:@"description2"]; [arrayOfLabels addObject:@"description3"]; [arrayOfLabels addObject:@"description4"]; [arrayOfLabels addObject:@"description5"]; [arrayOfLabels addObject:@"description6"];` And this we also created is not so difficult, now the most important thing. To display all this chaos, we need only one delegate method of a peak (); Here he is:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {} Inside the curly braces we write this:
UILabel *ourLabel = [[UILabel alloc] init]; // вот она наша меточка; ourLabel.frame = CGRectMake(-30, -40, 80, 80); // позиция меточки; [ourLabel setTextAlignment:UITextAlignmentLeft]; // выравнивание текста внутри метки; [ourLabel setFont:[UIFont fontWithName:@"Helvetica" size:10]]; ourLabel.numberOfLines = 2; [ourLabel setText:[arrayOfLabels objectAtIndex:row]]; // расставляем меточки строго по "строкам" [ourLabel setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:0]]; UIImage *check = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[imageArray objectAtIndex:row]]]; // ну тут все по аналогии checkView = [[UIImageView alloc] initWithImage:check]; checkView.frame = CGRectMake(-65, -10, 30, 20); [checkView addSubview:ourLabel]; customView = [[UIView alloc] init]; [customView addSubview:ourLabel]; //NSLog(@"Selected Value %@", [pickerView selectedRowInComponent:0]); //if(selectedRowInPicker == row){} [customView addSubview:checkView]; return customView; return [arrayOfLabels objectAtIndex:row];` Do not forget that all the arrays are initialized in the ViewDidLoad{} method, and lastly, if you have dofig pictures, then you should not hammer them with such a hardcore method, you can also hammer them into an array with a simple cycle :) Well, if I did not mess up then everything should work as it should. Do not forget to release arrays in the method - (void)dealloc{} . There will be problems, ask questions (some of the material was taken from the site stackoverflow).
- onePlease complete all your comments in a separate reply. By topic: in fact, UIPickerView is the same UITableView (although it is not inherited explicitly from it). So yes, the way to set the contents of the cells is the same, you have correctly written everything. - VioLet
- Well, sort of designed as necessary. - Red Rocket