【问题标题】:How do I populate Custom Cell's UIPickerView如何填充自定义单元格的 UIPickerView
【发布时间】:2019-04-09 13:22:13
【问题描述】:

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

但我的数据在主 VC 类中,我如何从 VC 填充选择器视图?

我是目标 c 的新手,如果没有清楚地解释我的问题,我很抱歉

我已经尝试在我的 VC 类中获取单元格并在 VC 类中填充单元格的选取器视图,但这不起作用,因为我无法在外部访问单元格的字段 - (__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;
}

【问题讨论】:

    标签: ios objective-c uicollectionview mobile-application


    【解决方案1】:

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

    在UICollectionView下面的d​​atasource中,符合UITextfieldDelegate

    (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ///....
    cell.textfield.delegate = self; 
    }
    

    现在实现 textfield 的委托方法并为 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 //selectedCell.ssrTextField.text = listOfOutboundSSR[key];

    【讨论】:

    • 感谢 Gurdeep 的快速响应,但我刚刚在pickerview didselectrow 方法更改后重新加载了我的collectionview.. 它工作正常。
    • 顺便说一句 @gurdeep 你介意解释一下为什么在这里使用 CGPoint 以及什么是 CGPoint tfPosition = [sender convertPoint:CGPointZero toView:self.ouboundCollectionView];此行中的发件人是什么?
    • 是的,如果您在 itemForIndexPath: 方法中有以下行,则重新加载 collectionview 也将起作用。 cell.ssrTextField.text = listOfOutboundSSR[key];但是有一个问题,您正在重新加载整个集合视图,而只有一个单元格发生了变化。理想情况下,您应该只重新加载该特定单元格。为此,您需要再次知道要重新加载哪个单元格,从而回到原来的问题。 .关于您的第二条评论,我刚刚编辑了我的答案。 sender 实际上是文本字段 :) 我用它来查找正在编辑哪个单元格的文本字段。
    • 是的,你是对的,我使用了你的方法,它就像魅力一样,是的,我使用了文本字段而不是发件人。干杯!谢谢你
    • - (void)textFieldDidBeginEditing:(UITextField *)textField { CGPoint tfPosition = [textField convertPoint:CGPointZero toView:self.collectionView]; NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tfPosition]; selectedCell = [self.collectionView cellForItemAtIndexPath: indexPath];如果我有多个 collectionView,这将如何工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多