繁体   English   中英

如何填充自定义单元的UIPickerView

[英]How do I populate Custom Cell's UIPickerView

我想填充UICollectionView的单元格。 我创建了一个自定义单元格,该单元格由Label和TextFiled组成,它们的inputView我已以编程方式更改为UIPickerView,因此其行为类似于pickerview,我的问题是我具有自定义单元格.h和.m文件,并且我有一个Controller类,collectionview是工作正常,但我无法将数据添加到UIPickerview中,因为它位于单独的.h文件中,因此它是pickerview委托,数据源方法将位于.m文件中。

但是我的数据在主要的VC类中,如何从VC填充pickerview?

我是目标c的新手,对不起,如果没有清楚说明我的问题,

我已经尝试在VC类中获取单元格并在VC类中填充单元格的pickerview,但这将无法正常工作,因为我无法访问外部的单元格字段- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{}方法。

  - (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;
}

您需要在Controller中创建一个属性,即selectedCell,并将DataType作为自定义单元格类类型。

在UICollectionView的以下数据源中,遵循UITextfieldDelegate

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

///....
cell.textfield.delegate = self; 
}

现在实现文本字段的委托方法,并将值分配给selectedCell

- (void)textFieldDidBeginEditing:(UITextField *)textField {

   CGPoint tfPosition = [textField convertPoint:CGPointZero toView:self.collectionView];
   NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tfPosition];
   selectedCell = [self.collectionView cellForItemAtIndexPath: indexPath];
}

现在,您可以在UIPIckerView数据源的Delegate方法中使用selectedCell //selectedCell.ssrTextField.text = listOfOutboundSSR[key];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM