How to use a UITableView to return a value

How to use tableView as a value selector?

So, I have a number of input fields, and I want, when you select the cetian field, it opens a table view of the parameters that you can select from the value for this field. After selecting an option, it returns to the previous view with the selected value filling this field.

+3
source share
3 answers

This is what I am doing, similar to the settings> General> International> Language table in iPhone / iPod.

table view

The user can touch the line and a checkmark will appear. The view closes when you click Finish or Cancel.

UITableViewController . "" "". :

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

presentModalViewController:animated: , . , iPhone.

target selector , "".

UITableViewController you can implement the the tableView: didSelectRowAtIndexPath: ':

- (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
}

:

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

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

UITableViewDelegate tableView:didSelectRowAtIndexPath:, - ( share/singleton, , - ), .

+1

I implemented the choice of ViewController for Date. I am creating a protocol to return the date selected in 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 selection mode you can use

tableView:didSelectRowAtIndexPath:

to select the desired line. Here I set the data property.

The previous submission is the delegate for the protocol.

+1
source

Source: https://habr.com/ru/post/1728855/


All Articles