UIPickerView - 显示图像和文本

时间:2013-06-25 23:30:33

标签: 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;

}

1 个答案:

答案 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 如果有人知道一种更简单的方法,或者可以回答我原来的问题,那就是超级。