使用 UIPickerView 的选定行来确定要添加到哪个数组

发布于 2024-10-21 02:12:23 字数 816 浏览 4 评论 0原文

因此,我设置了一个 UIPickerView 来根据选择的组件显示不同的 UITableView 数据。该表由数组填充,而 pickerview 基本上只是告诉表要使用哪个数组。

我希望用户能够向每个单独的数组添加信息,从而更改每个单独的表格视图。但是,我在访问我创建的 IBAction 中的 PickerView 行时遇到了一些问题。

我有一个“添加”按钮,它调用 IBAction 将用户输入添加到数组中。我遇到麻烦的地方是告诉操作将输入添加到哪个数组。

-(IBAction)postToWall:(id)sender {


[userInput resignFirstResponder];
NSString *userInputString;
userInputString=userInput.text;


[otherPosts addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:userInputString,@"post", @"Jeff.png",@"picture",nil]];

[wallTable reloadData];
[userInput setText:@" "];
[sendButton setEnabled:NO];

我的直觉是添加一个 If 语句,它基本上表示“如果 pickerview 行为 0,则添加到此数组”和“如果 pickerview 行为 1,则添加到该数组 这可能吗?我如何与 pickerview 对话并设置 if 语句?抱歉,我对这一切有点陌生。

谢谢大家真的很感激!

So I've set up a UIPickerView to display different UITableView Data depending on which component is selected. The table is populated by arrays, and the pickerview basically just tells the table which array to use.

I want the user to be able to add info to each individual array and thus change each individual tableview. However, im having a little trouble accessing the PickerView row within the IBAction i've created.

I have a "add" button that calls an IBAction that adds the user input to the array. Where I am having trouble is telling the action which array to add the input to.

this is what I have so far:

-(IBAction)postToWall:(id)sender {


[userInput resignFirstResponder];
NSString *userInputString;
userInputString=userInput.text;


[otherPosts addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:userInputString,@"post", @"Jeff.png",@"picture",nil]];

[wallTable reloadData];
[userInput setText:@" "];
[sendButton setEnabled:NO];

}

My intuition is to add an If statement, which basically says "if pickerview row is 0, then add to this array," and "if pickerview row is 1, then add to that array." Is this possible? How do I talk to the pickerview and set up that if statement? Sorry im a little new to all this.

Thanks guys really appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑看君怀她人 2024-10-28 02:12:23

我的建议是,

创建一个适用于表视图数据源的数组。当您选择为显示和调用数据源而创建的选择器的任何行时,意味着类似这样的事情,

在 .h 中声明一个数组

NSMutableArray *dataArray;

,使其属性并合成它

并在viewDidLoad中分配它并释放它dealloc。

并在选择器视图委托中

 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

     if(row==0)
       {
           [dataArray addObjectsFromArray:Array1];
      }
    else
    {
      [dataArray addObjectsFromArray:Array2];
    }

 [self.table reloadData];
}

My suggetion is,

Make a single array which works data source for your table view.When you select any row of picker your array created for showing and call data source means some thing like this,

declare a array in .h

NSMutableArray *dataArray;

make it property and synthesize it

and alloc it in viewDidLoad and release it dealloc.

and in picker View delegate

 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

     if(row==0)
       {
           [dataArray addObjectsFromArray:Array1];
      }
    else
    {
      [dataArray addObjectsFromArray:Array2];
    }

 [self.table reloadData];
}
北城挽邺 2024-10-28 02:12:23

如果我是你,我只需将 currentArray 变量添加到我的代码中,并在每次 PickerView 更改值时将各个数组设置为此 currentArray。

并使用 [currentArray addObject: ];在你的 -(IBAction)postToWall:(id)sender 方法中

if I were you I just add currentArray variable to my code and set your individual arrays to this currentArray every time when my PickerView changed value.

and use [currentArray addObject: ]; in your -(IBAction)postToWall:(id)sender method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文