简体   繁体   中英

Using selected row of UIPickerView to determine which Array to Add to

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!

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];
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM