Fixed labels in the selection bar of a UIPickerView

前端 未结 12 1226
一生所求
一生所求 2020-11-27 12:17

In the clocks application, the timer screen shows a picker (probably a UIPicker in UIDatePickerModeCountDownTimer mode) with some text in the selec

12条回答
  •  一向
    一向 (楼主)
    2020-11-27 12:53

    Let's say we want to implement a picker view for selecting distance, there are 2 columns, one for distance, one for unit, which is km. Then we want the second column to be fixed. We can make it through some delegate methods.

    - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        if (component == 0) {
            return self.distanceItems[row];
        }
        else {
            return @"km";
        }
    }
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 2;
    }
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        if (component == 0) {
            return [self.distanceItems count];
        }
        else {
        // when it comes to the second column, only one row.
            return 1;
        }
    }
    

    Now we have this: enter image description here

    I guess this is the simplest way.

提交回复
热议问题