jsetting32
jsetting32

Reputation: 1632

Passing data back from detailViewController to UITableView

I am currently at the issue where I have a UITableviewCell that needs to be updated.

When the user presses on the uitableviewcell - THERES ONLY 1!!, the user is pushed to a UITABLEVIEWCONTROLLER where the user is allowed to select 1 of multiple cells with their own titles.

I need to get the clicked tableviewcells title and pass the value back to the parentviewcontroller and update the 1 tableviewcell's name to the one the user clicked in the pushed uitableivewcontroller.

Here is a picture of the parent viewcontroller... Parent Controller

And heres the picture of the pushed viewcontroller.... Child Controller

I was told earlier yesterday that delegation would be needed but I am unsure what to do at this point :/.

Heres some code I use in the parent viewcontroller...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProblemTypes *view = [[ProblemTypes alloc] init];
    [self.navigationController pushViewController:view animated:YES];

}

I am also NOT USING storyboards, just a few xibs.

Also heres the code for the pushedviewcontroller to pop to the parent viewcontroller when a cell is selected...

#pragma mark - Table view delegate

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"Cell's text: %@",cell.textLabel.text);

    [self.navigationController popViewControllerAnimated:YES];
}

Thank you guys!

Upvotes: 0

Views: 1207

Answers (1)

jsetting32
jsetting32

Reputation: 1632

Found it out.... Do delegation was the solution... Just hope it's the MOST EFFICIENT!!! Here's the code for delegation.

First, implement the delegate of the parentViewcontroller and it's methods, also make sure to add delegation to the parentviewcontroller...

@protocol SendFeedBackDelegate

- (void) didReceiveType:(NSString *) message;

@end
@interface SendFeedBackViewController : UIViewController <SKPSMTPMessageDelegate, UITableViewDataSource,UITableViewDelegate, SendFeedBackDelegate>
{
    NSString *subject;

}

Next, implement the method : - (void) didReceiveType:(NSString *) message; under

@implementation SendFeedBackViewController

- (void) didReceiveType:(NSString *) message
{
    subject = message;
    [feedbackTableView reloadData];
    // I reload the data because it is needed when this function is going to be called
    // in the child viewcontroller.... just keep reading :)
}

Now go to - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath for the use of this example and my project :)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [feedbackTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // THIS IS THE IMPORTANT PIECE OF CODE YOU NEED TO NOTICE.....
    // it allows for the first thing the tableview cell to be is a static string until subject 
    // it is changed and the user chooses a subject in the childviewcontroller
    if (subject == nil) {
        cell.textLabel.text = @"Select a Product";
    } else {
        cell.textLabel.text = subject;
    }


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Now, add a protocol to the childviewcontroller to allow the childviewcontroller to conform to the parentviewcontroller

In childviewcontroller.h: add these lines of code,

#import "ParentViewController.h"

@protocol SendFeedBackDelegate;

@interface FeedbackTypes : UITableViewController
{
    id<SendFeedBackDelegate> delegate;
}

@property (nonatomic, assign) id<SendFeedBackDelegate> delegate;

Now you have set the delegate in the parent viewcontroller.... Next head over the same files implementation file (.m) and add these:

//Add synthesize just under @implementation "ClassName"

@synthesize delegate;

// I used a uitableviewcontroller for this example so refer to the problem I have above
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //NSLog(@"Cell's text: %@",cell.textLabel.text);
    [delegate didReceiveType:cell.textLabel.text];
    [self.navigationController popViewControllerAnimated:YES];
}

And THATS IT!!!!..... :), Hope this was a simple and basic tutorial and here's a snapshot.

BOO YA!!!!

Upvotes: 1

Related Questions