Cam
Cam

Reputation: 285

iOS:UITableView & Back Button, how to update cell in parent view?

I searched, but didn't find good answer to this:

It should be pretty straightforward, I navigate from a parent TableView to a child TableView that implements only the selection option for a cell in parent View.

In childTableView, I would like to pass back the value chosen by user in child view (selection one of the cell in this view) to parent view and update the "cell.detailTextLabel.text" in parent view, I can find when the back button in child view got tapped but cannot find how/where to update the cell in parent view with new value got from child view's cell.

My second question is how to pass the data to parent view.

Upvotes: 1

Views: 1578

Answers (1)

greenhorn
greenhorn

Reputation: 1107

Try the following

In child view, define a protocol

// childViewController.h
@protocol ChildDelegate;

@interface childViewController
{
    id <ChildDelegate> delegate;
}

@property (nonatomic, weak) id <ChildDelegate> delegate;
@end // interface

@protocol ChildDelegate <NSObject>
    - (void) selectedData: (NSString*) text;
@end

Send data when user selects a cell

// childViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    ...
    // send info the mainViewController
    [self.delegate selectedData: (NSString*) [dataSource objectAtIndex: indexPath.row]];
}

In mainViewController, implement this protocol

- (void) selectedData: (NSString*) text
{
   // retrieve current selected row, and update with text.
}

// don't forget to set the delegate when you create the childViewController

child = // create child
child.delegate = self;
// save currently selected row
// push childViewController

Hope it helps.

Upvotes: 1

Related Questions