gpt4 book ai didi

ios - 如何填充自定义单元格的 UIPickerView

转载 作者:行者123 更新时间:2023-11-28 23:34:14 28 4
gpt4 key购买 nike

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

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

我是 Objective 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;
}

现在实现textfield的delegate方法并赋值给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 数据源中使用 selectedCell,委托(delegate)方法//selectedCell.ssrTextField.text = listOfOutboundSSR[key];

关于ios - 如何填充自定义单元格的 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55593965/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com