2014-01-16 42 views
2

問題是,選擇器不顯示任何數據,但數據存在於背景中,因爲我在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; 

} 
+0

我覺得填充數組後,你不重裝pickerview – Ashutosh

+0

你應該調用'[picker reloadAllComponents];'重新加載選擇器。 – Akhilrajtr

+0

已經嘗試過,但沒有用 – Ali

回答

3

問題是

- (UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView *) view 

如果刪除它正常顯示的部分。

希望它能幫助你一點。

我也找到根本原因。如果我有更多的信息,我會更新答案。

/////////////////

我找到了原因。 將UILabel init部分更改爲

UILabel *label = [[UILabel alloc] init]; 

這樣就可以了。

+0

哦,是的,謝謝史蒂文 – Ali

2

試試這個....我覺得你的選擇器的高度是零

picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 200)]; 

    [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]; 
+0

與之前相同@shaik riyaz – Ali

+0

沒有問題與選擇器只有與它不顯示的數據,但當我滾動空選擇器選擇更改爲nslog。意味着只顯示數據 – Ali

2

試試這個

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]; 
[picker reloadAllComponents]; 
+0

與以前不一樣 – Ali

+0

嘗試刪除titleForRow:委託方法的UIPickerView – Akhilrajtr

+0

完成但與以前相同不顯示數據的選擇器,但滾動更改日誌中的數據 – Ali