【问题标题】:UIPickerview not calling titleForRowUIPickerview 没有调用 titleForRow
【发布时间】:2014-07-12 00:27:21
【问题描述】:

我有一个 uipickerview,我正在尝试使用 coredata 数据库中的博士列表填充它。我正在 viewdidload 中加载博士列表,并通过 nslog 语句验证了它们的存在。在验证它们在那里之后,我执行了 reloadallcomponents 并且我的pickerview 显示了适当数量的“?”分数。当我设置断点时,我注意到我的应用程序通过两次获取行数,并返回适当的数字,但 titleforrow 方法从未被调用。有什么想法吗?

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    NSLog(@"there are %d doctors",[self.doctors count]);
    return [self.doctors count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component
{
    NSLog(@"Row %d has dr %@",row,[self.doctors objectAtIndex:row]);
    return [self.doctors objectAtIndex:row];
}

【问题讨论】:

  • 除了dataSource之外,您是否设置了选择器视图的delegate
  • titleForRow:numberOfRowsInComponent:/numberOfComponentsInPickerView: 不是所有数据源方法吗?
  • @nhgrif 你在问我吗?看文档。
  • 嗯,好像没有。两个numberOf 方法在datasource 中。其他所有内容都在delegate 中。
  • 我已将类设置为数据源和委托。那应该没问题。对?? @interface DBEPatientVC ()

标签: ios objective-c uipickerview


【解决方案1】:

您必须将 pickerView.dataSource = ... 设置为您的控制器;

【讨论】:

  • 关闭,必须设置委托。
【解决方案2】:

感谢伊戈尔的回答,我走上了正确的道路。我必须在 ViewDidLoad 中将委托设置为 self。

我已经为类中的选择器视图创建了一个名为医生PV 的出口,并且由于组件和行方法被正确调用,我认为委托是通过包含 pickerviewdelegate 协议来推断的。我猜不是。

我将 self.doctorPV.delegate=self 添加到我的 viewDidLoad 中,一切都像魔术一样工作。

谢谢伊戈尔。

【讨论】:

    【解决方案3】:

    .h

    #import <UIKit/UIKit.h>
    @interface PickerViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
    

    .m

    - (void)viewDidLoad {
          [super viewDidLoad];
          your picker view controller.delegate=self;
        your picker view controller.dataSource=self;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多