将静态行添加到UIPickerView

时间:2016-10-21 19:15:19

标签: ios uitextfield uipickerview

在我的应用程序中,我查询Parse.com以获取填充UIPickerView的组列表。我想在最顶层的UIPickerView中添加一个名为“Create New Group”的选项,其中它将拉出一个UITextField以创建一个新组并从那里移动。如何将其添加到所有其他字段?到目前为止,我是如何创建它的。

-(void)addPickerView{

     __block NSMutableArray *arr = [[NSMutableArray alloc] init];

    PFQuery *rejectedNumber = [PFQuery queryWithClassName:@"Group"];
    [rejectedNumber orderByAscending:@"GroupName"];
    [rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {



        if (!objects) {
            // Did not find any UserStats for the current user

            NSLog(@"NotFound");
        } else {
            pickerArray = objects;
            [myPickerView reloadAllComponents];

        }
    }];

     //pickerArray = GoGo;
    self.theView.signUpView.additionalField.delegate = self;
    [self.theView.signUpView.additionalField setPlaceholder:@"Choose Group"];
    myPickerView = [[UIPickerView alloc]init];
    myPickerView.dataSource = self;
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                                   target:self action:@selector(finishIt)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                          CGRectMake(0, self.view.frame.size.height-
                                     self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
    [toolBar setBarStyle:UIBarStyleBlackOpaque];
    NSArray *toolbarItems = [NSArray arrayWithObjects:
                             doneButton, nil];
    [toolBar setItems:toolbarItems];
    self.theView.signUpView.additionalField.inputView = myPickerView;
    self.theView.signUpView.additionalField.inputAccessoryView = toolBar;

}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count];
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
    [self.theView.signUpView.additionalField setText:self.theGroup];
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    PFObject *object = pickerArray[row];
    //Assuming "Category" is a string here for your title
    self.theGroup = object[@"GroupName"];
    return object[@"GroupName"];
}

1 个答案:

答案 0 :(得分:1)

您可以通过两种方式添加实现:

  1. 在数组的第一个索引处添加一个额外的对象,以显示"创建新组"
  2. 您可以使用调整数据源并委托UIPickerView的方法。像:
  3. -(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component
    {
        return [pickerArray count] + 1; // +1 for the additional row
    }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:
    (NSInteger)row inComponent:(NSInteger)component
    {
        if (row == 0)
        {
            // Create New Group clicked
        }
        else
        {
           // Some other option selected
        }
    }
    
    - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        NSString *groupName  = "Create New Group";
        if (row != 0)
        {
            PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
            self.theGroup    = object[@"GroupName"];
            groupName        = object[@"GroupName"];
        }
        return groupName;
    }