当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - UIPickerView pickerView:titleForRow:rowforComponent method not called

问题描述:

I have a UIPickerView in my storyboard.

The UIViewController containing it has interface declarations set

@interface ContactDetail : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate,UIAlertViewDelegate, UITextFieldDelegate>

on view Load, pickerview delegation set

pickerView_RelationshipType.dataSource=self;

pickerView_RelationshipType.delegate = self;

pickerView_RelationshipType.showsSelectionIndicator = YES;

all UIPickerView datasource and delegate methods are triggered except those 2

pickerView:titleForRow:rowforComponent

and

pickerView:viewForRow:forComponent:reusingView

those following are called, no problem

 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

网友答案:

what's in numberOfComponentsInPickerView ?

If your numberOfRows returning 0, then your delegate method will never be called - the picker won't ask for the title of a row if it doesn't think it has any rows to display.

网友答案:

Make sure your data source array is allocated and initialized. I ran into this problem when everything was hooked up correctly (I set the data source and delegates and was conforming to the protocols), but I forgot to initialize my data source arrays before adding objects to them. My code was hitting numberOfComponentsInPickerView and numberOfRowsInComponent but not titleForRow and I couldn't figure out why. Make sure your data source is not returning nil.

Kind of a stupid edge case, but I figured I'd add my answer in case someone else runs into the same issue.

分享给朋友:
您可能感兴趣的文章:
随机阅读: