2013-06-25 22 views
0

是否可以在同一個選取器視圖中顯示行的行標題和行的視圖?我想要做的是讓人們能夠爲我正在編寫的時間表應用程序選擇一個主題房間號和顏色。我希望顏色選擇是一個圖像。它顯示的圖像,但不是標題。UIPickerView - 顯示圖像和文字

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     
forComponent:(NSInteger)component 
{ 
    if (component == kSubjects) return [PSubjects objectAtIndex:row]; 
    if (component == kDay) return [PDays objectAtIndex:row]; 
    //if (component == kLesson) return [PLessonPeriods objectAtIndex:row]; 
    return [PRoomChoices objectAtIndex:row]; 

    NSString *title; 
    title = [@"" stringByAppendingFormat:@"%d",row]; 

    return title; 
} 

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: 
(NSInteger)component reusingView:(UIView *)view 
{ 
    if (component == kLesson) { 
    UIImageView *myIcon = [[UIImageView alloc] initWithImage:[PLessonPeriods   
    objectAtIndex:row]]; 
    [myIcon setFrame:CGRectMake(0, 0, 50, 20)]; 
    return myIcon;} 

    else return 0; 

} 

回答

0

嗯,我偷偷摸摸地創建了一個UIView,然後添加了一個UILabel。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    // This returns the images 
    if (component == kLesson) { 
    UIImageView *myIcon = [[UIImageView alloc] initWithImage:[PLessonPeriods objectAtIndex:row]]; 
    [myIcon setFrame:CGRectMake(0, 0, 50, 20)]; 
    return myIcon;} 

    // This adds a blank UIIamgeView with a label added as a subview. 
    if (component == kSubjects) { 
     UIImageView *myIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 122, 20)]; 
     UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 122, 20)]; 
     myLabel.text = [PSubjects objectAtIndex:row]; 
     myLabel.backgroundColor = [UIColor clearColor]; 
     [myIcon addSubview:myLabel]; 
     return myIcon; 
    } 
    else return 0; 

} 

enter image description here 如果有人知道更簡單的方法,也可以回答我原來的問題,這將是超級計算機。