2015-10-07 5 views
0

У меня работает UIPicker. Только проблема заключается не только в том, что мне нужен текст для каждой строки, но мне также нужно изображение с левой стороны текста и некоторый дополнительный подтекст в основном тексте. Я понятия не имею, как это сделать, поэтому я надеялся, что кто-то может указать мне в правильном направлении? Я бы очень признателен. Вот как это должно выглядеть: (! Взглянуть на него)Пользовательский UIPicker - размещение изображения, текста и подтекста в одной строке?

enter image description here

+1

Попробуйте следующее: https://github.com/nicklockwood/CountryPicker –

+0

Спасибо, господин Т! Этот подкласс работает как магия. – Krekin

ответ

1

Глядя на проект под названием «CountryPickerDemo», что связано с Mr.T я понял метод основной для создания изображения & текст отображается в одной строке:

- (UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) строки forComponent: (NSInteger) компонент reusingView: (UIView *) вид

Здесь как я сделал это работать в моем коде:

//Custom view and rows are created inside this method. There doesn't seem to be an easier way. 
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    //IF view doesn't exist, then create it. 
    if (!view) 
    { 
     //View creation. 
     view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)]; //(0, 0, 280, 30)]; //x & y width & height 

     //Rows label creation. 
     UILabel *label   = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 110, 24)]; //(35, 3, 245, 24)]; 
     label.textColor   = [UIColor blackColor]; 
     label.tag    = 1; 
     [view addSubview:label]; 

     //Rows image creation. 
     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)]; //(3, 3, 24, 24)]; 
     imgView.contentMode = UIViewContentModeScaleAspectFit; 
     imgView.tag   = 2; 
     [view addSubview:imgView]; 
    } 

    //INPUT data into rows. P.S. I have TWO components; not just one like in the picture above. If you only need one component like in the picture above, then ignore the IF ELSE statements and just use the code inside the IF statement only. 
    if (component == 0) 
    { 
     //Load actual text into 1st label sections. 
     ((UILabel*)[view viewWithTag:1]).text = [NSString stringWithFormat:@"%@", _componentOne[row]]; 

     //Load actual images into 1st image sections. 
     ((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:@"imageName.png"]; 
    } 
    else 
    { 
     //Load actual text into 2nd label sections. 
     ((UILabel*)[view viewWithTag:1]).text = [NSString stringWithFormat:@"%@", _componentTwo[row]]; 

     //Load actual images into 2nd image sections. 
     ((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:@"imageName.png"]; 
    } 



    //EXAMPLE CODE.  
    //((UILabel*)[view viewWithTag:1]).text  = [[self class] countryNames][(NSUInteger)row]; 

    //NSString *imagePath = [NSString stringWithFormat:@"CountryPicker.bundle/%@", [[self class] countryCodes][(NSUInteger) row]]; 
    //((UIImageView*)[view viewWithTag:2]).image = [UIImage imageNamed:imagePath]; 


    return view; 
} 

ПРИМЕЧАНИЕ: Введите свое имя изображения в заполнитель @ «imageName.png», чтобы увидеть, как она выглядит. Размещение разных изображений, возможно, используя массив, - это ваша сделка, чтобы понять.

Смежные вопросы