Affian
Affian

Reputation: 3398

How to use a UITableView to return a value

How would I use a tableView as a value selector?

So I have a series of input fields and what I want is when you select a cetian field it opens a tableview of options that you can pick from as a value for that field. Upon selecting an option it returns to the previous View with the selected value filling that field.

Upvotes: 3

Views: 3159

Answers (3)

Marco Mustapic
Marco Mustapic

Reputation: 3859

This is what I do, similar to the Settings > General > International > Language table view in the iPhone/iPod.

table view

The user can tap a row and a check mark will appear. The view is dismissed when "Done" or "Cancel" is tapped.

First, create a UITableViewController that will display your options. Have a toolbar on the top with a Cancel and Done button. Also have these properties:

SEL selector;   // will hold the selector to be invoked when the user taps the Done button
id target;      // target for the selector
NSUInteger selectedRow;   // hold the last selected row

This view will be presented with the presentModalViewController:animated: method so it appears from the bottom of the screen. You could present it in any other way, but it seems kind of standard across iPhone applications.

Before presenting the view, set target and selector so a method will be called when the user taps the "Done" button.

Now, in your newly created UITableViewController you can implement the thetableView:didSelectRowAtIndexPath:` method as:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;  // show checkmark
    [cell setSelected:NO animated:YES];                      // deselect row so it doesn't remain selected
    cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]];     
    cell.accessoryType = UITableViewCellAccessoryNone;       // remove check from previously selected row
    selectedRow = indexPath.row;                             // remember the newly selected row
}

Also implement cancel and done methods for the toolbar buttons:

- (IBAction)done:(UIBarButtonItem *)item
{
    [target performSelector:selector withObject:[stringArray objectAtIndex:selectedRow]];
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancel:(UIBarButtonItem *)item
{
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 6

Farlei Heinen
Farlei Heinen

Reputation: 5899

I implemented a ViewController for Date pick. I create a protocol to return the date picked to the previous view.

@protocol DataViewDelegate
@optional
- (void)dataViewControllerDidFinish:(NSDate*)dateSelected;
@end

...

- (void) viewDidDisappear:(BOOL)animated
{
    if ([ (id)(self.delegate) respondsToSelector:@selector(dataViewControllerDidFinish:)])
    {
        [self.delegate dataViewControllerDidFinish:self.data];
    }

    [super viewDidDisappear:animated];
}

In the picker view you can use the

tableView:didSelectRowAtIndexPath:

to select the row you want. Here i set the data property.

The previous view is the delegate for the protocol.

Upvotes: 1

Adam Woś
Adam Woś

Reputation: 2473

You should use UITableViewDelegate's tableView:didSelectRowAtIndexPath:, remember the value somewhere in another object (a share instance/singleton maybe? - depending on your architecture) and then dismiss this table view.

Upvotes: 0

Related Questions