UIPickerView 更改selectRow样式

注意事项: 手动调用 selectRow: inComponent: animated: 不会触发 - pickerView:didSelectRow:inComponent: 的调用

对于2列的UIPickerView,样例代码

  • 初始化中
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
        [self pickerView:self.pickerView didSelectRow:0 inComponent:1];
    });
  • 返回自定义的样式
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (id)view;
    
    if (component == 0) {
        if (!label) {
            label = [UILabel snl_labelWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.25 + 5, 40) title:nil fontSize:18 textColor:UIColorFromHex(0xcbcbcb)];
            label.textAlignment = NSTextAlignmentCenter;
        }
    }
    else
    {
        if (!label) {
            label = [UILabel snl_labelWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.75 + 5, 40) title:nil fontSize:18 textColor:UIColorFromHex(0xcbcbcb)];
            label.textAlignment = NSTextAlignmentCenter;
        }
    }
    
    return label;
}
  • 对于uipickerview滚动的处理
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:DefaultAppColor];
    labelSelected.backgroundColor = [UIColor whiteColor];
    
    if (component == 0) {
         // 更改component=1的数据源
        //self.timeslots = self.daytimes[self.days[row]];
        
        [self.pickerView reloadComponent:1];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.pickerView selectRow:0 inComponent:1 animated:YES];
            [self pickerView:self.pickerView didSelectRow:0 inComponent:1];
        });
    }
}

你可能感兴趣的:(UIPickerView 更改selectRow样式)