gpt4 book ai didi

ios - 从空白行开始 UIPickerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:08 26 4
gpt4 key购买 nike

我的应用程序从服务器中提取数据以获取可供选择的当前组列表。然后它在这个名为“创建新组”的顶部添加一行。这很好用,但它开始时已经选择了这一行,所以我必须离开它,然后再次返回以选择它并激活键盘。我怎样才能再添加一个选项让它以空白行开头?我曾尝试简单地在行中执行 + 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;
}

最佳答案

此方法将您的数组调整为两个,允许您请求的空白空间。我还引入了 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;
}
}

我包含了下面的图片,以显示使用一些通用数据时的样子。

enter image description here

关于ios - 从空白行开始 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40270964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com