我的应用程序从服务器提取数据以获取可供选择的当前组列表。然后它在这个名为"创建新组"的顶部添加一行。这很好用,但它开始已经选择了这一行,所以我必须远离它,然后再返回选择它并激活键盘。如何添加一个选项以使其以空行开头?我试过简单地在行中做+ 2而不是+ 1,然后将所有内容排序,但它根本不起作用。非常感谢任何帮助。
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count] + 1;
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
if (row == 0)
{
[self.theView.signUpView.additionalField resignFirstResponder];
self.theView.signUpView.additionalField.inputView = nil;
[self.theView.signUpView.additionalField becomeFirstResponder];
}
else {
[self.theView.signUpView.additionalField setText:self.theGroup];
}
}
- (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;
}
答案 0 :(得分:1)
此方法将数组调整为2,允许您请求的空白区域。我还介绍了switch语句,以便更容易理解发生了什么。代码中添加了注释,以突出显示与您的请求相关的更改。
#pragma mark- Picker View Delegate
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count] + 2; // This offsets your blank line & "Create new group" option
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
switch (row) {
// Ignore 0 here since you don't want to handle the blank space
case 1:
[self.theView.signUpView.additionalField resignFirstResponder];
self.theView.signUpView.additionalField.inputView = nil;
[self.theView.signUpView.additionalField becomeFirstResponder];
break;
default:
[self.theView.signUpView.additionalField setText:self.theGroup];
}
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (row) {
case 0:
return @"";
case 1:
return @"Create New Group";
default:
PFObject *object = pickerArray[row - 2];
self.theGroup = object[@"GroupName"];
return self.theGroup;
}
}
我在下面的图片中显示了使用某些通用数据时的效果。
答案 1 :(得分:0)
其他想法:
使用按钮添加到选择器InputAccessoryView。并在此按钮上添加操作,并删除didSelect中的逻辑。