I wanted to populate UICollectionView's Cells. I have created a custom cell which consists of a Label and TextFiled whose inputView I have changed to UIPickerView programmatically so it will behave like pickerview, my question is I have custom cell .h and .m file and I have one Controller class, collectionview is working fine but I am unable to add data to UIPickerview as it is in indvidual .h file so it's pickerview delegate and datasource methods will be in it's .m file.
But my data is in main VC class how do I populate the pickerview from VC?
I am new to objective c and sorry if not explained my problem clearly
I have already tried to get cell in my VC class and populate cell's pickerview in VC class but this would not work because I can not access cell's fields outside - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{}
method.
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(pickerView.tag==0){
return outboundBaggages.count;
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(pickerView.tag==0){
NSString *key = [NSString stringWithFormat:@"%ld",(long)row];
ssrDescription = listOfOutboundSSR[key];
//cell.ssrTextField.text = listOfOutboundSSR[key];
//above line should be the right but it is field of cell and i
//can not access it outside of collectionview's method
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(pickerView.tag==0){
NSString *key = [NSString stringWithFormat:@"%ld",(long)row];
return listOfOutboundSSR[key];
}
return nil;
}
You'll need to create a property in the Controller, i.e. selectedCell with DataType as your custom cell class type.
In UICollectionView's below datasource, conform to UITextfieldDelegate
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
///....
cell.textfield.delegate = self;
}
Now implement textfield's delegate method and assign value to selectedCell
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGPoint tfPosition = [textField convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tfPosition];
selectedCell = [self.collectionView cellForItemAtIndexPath: indexPath];
}
Now, you can use selectedCell in UIPIckerView Datasource, Delegate Methods
//selectedCell.ssrTextField.text = listOfOutboundSSR[key];