問題是,選擇器不顯示任何數據,但數據存在於背景中,因爲我在nslog中滾動空選擇器視圖時顯示數據。所以數據正在填充,但不會顯示在uipickerview中。在此先感謝UIPickerView不顯示選擇器中的數據,但在nslog中顯示
這裏是我已經宣佈
UIPickerView *picker;
.m文件
picker = [
[UIPickerView alloc] initWithFrame: CGRectMake(0, 100, 320, 0)];
[picker setDataSource: self];
[picker setDelegate: self];
picker.showsSelectionIndicator = YES;
peoplearray = [
[NSArray alloc] initWithObjects: @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", nil];
timearray = [
[NSArray alloc] initWithObjects: @"12:00 AM", @"12:30 AM", @"1:00 AM", @"1:30 AM", @"2:00 AM", @"2:30 AM", @"3:00 AM", @"3:30 AM", @"4:00 AM", @"4:30 AM", @"5:00 AM", @"5:30 AM", @"6:00 AM", @"6:30 AM", @"7:00 AM", @"7:30 AM", @"8:00 AM", @"8:30 AM", @"9:00 AM", @"9:30 AM", @"10:00 AM", @"10:30 AM", @"11:00 AM", @"11:30 AM", @"12:00 PM", @"12:30 PM", @"1:00 PM", @"1:30 PM", @"2:00 PM", @"2:30 PM", @"3:00 PM", @"3:30 PM", @"4:00 PM", @"4:30 PM", @"5:00 PM", @"5:30 PM", @"6:00 PM", @"6:30 PM", @"7:00 PM", @"7:30 PM", @"8:00 PM", @"8:30 PM", @"9:00 PM", @"9:30 PM", @"10:00 PM", @"10:30 PM", @"11:00 PM", @"11:30 PM", nil];
類似的是日期數組中的.h文件中的代碼 。
-(NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView {
return 4;
}
-(NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component {
if (component == 0) {
return [peoplearray count];
} else if (component == 1) {
return [datearray count];
} else if (component == 2)
{
return [timearray count];
} else if (component == 3)
{
return [timearray count];
}
return 0;
}
-(NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component {
if (component == 0)
{
return [peoplearray objectAtIndex: row];
} else if (component == 1)
{
return [datearray objectAtIndex: row];
} else if (component == 2)
{
return [timearray objectAtIndex: row];
} else if (component == 3)
{
return [timearray objectAtIndex: row];
}
return 0;
}
- (CGFloat) pickerView: (UIPickerView *) pickerView widthForComponent: (NSInteger) component {
switch (component) {
case 0:
return 35;
case 1:
return 100;
case 2:
return 80;
case 3:
return 80;
}
return 0;
}
- (UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView *) view {
UILabel * label = [
[UILabel alloc] initWithFrame: CGRectMake(0, 0, 300, 37)];
if (component == 0)
{
label.text = [peoplearray objectAtIndex: row];
} else if (component == 1)
{
label.text = [datearray objectAtIndex: row];
} else if (component == 2)
{
label.text = [timearray objectAtIndex: row];
} else if (component == 3)
{
label.text = [timearray objectAtIndex: row];
}
label.backgroundColor = [UIColor clearColor];
return label;
}
我覺得填充數組後,你不重裝pickerview – Ashutosh
你應該調用'[picker reloadAllComponents];'重新加載選擇器。 – Akhilrajtr
已經嘗試過,但沒有用 – Ali