0

I'm creating a new app, and in the app i want two tableviews. One for showing a list, and another one to show the stuff the users picks from the list. So far i've created the arrays to contain the items, and i've gotten the views to pass between each other. I cannot seem to get the selected items shown in the other tableview.

Here's some code from the list-tableview (the data gets passed when 'Done' is pushed). viewC is an instance of the other view, used to get acces to it.

-(IBAction)dismissValgScene {
for (int i = 0; i < ValgteMedierarray.count; i++) {
    NSLog(@"%@", [ValgteMedierarray objectAtIndex:i]);
}
viewC.TilvalgteMedierArray = [NSMutableArray arrayWithArray:ValgteMedierarray];
NSLog(@"1: antal i tilvalgteArray = %lu", (unsigned long)viewC.TilvalgteMedierArray.count);
[viewC.tableView reloadData];
NSLog(@"2: antal i tilvalgteArray = %lu", (unsigned long)viewC.TilvalgteMedierArray.count);
[self dismissViewControllerAnimated:YES completion:nil];
}

This is the method for the Done button, which adds the selected items to an array in the other view. (that is the viewC.TilvalgteMedierArray)

Here is some code from the other view, which should show the selected items

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [TilvalgteMedierArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

// Configure the cell...
cell.textLabel.text = [TilvalgteMedierArray objectAtIndex:indexPath.row];

return cell;
}

Hope you guys can help, and please just ask me to elaborate whatever you don't understand of my coding :)

(I apoligize if this has been answered elsewhere, but i have been looking for hours without finding a solution).


EDIT:

A thank you to all who has been trying to help me. :)

I have now found a solution by using the Unwind Segue method. Anyone with a similar problem can look at these links:

How to make an unwind segue: What are Unwind segues for and how do you use them?

How to pass data with the unwind segue: iOS 6 - can i return data when i unwind a segue?

Community
  • 1
  • 1
  • `[NSMutableArray arrayWithArray:ValgteMedierarray];` should better be `[NSMutableArray arrayWithArray:[ValgteMedierarray copy]];` because the `arrayWithArray:` method doesn't expect a mutable version of an array. – Jörg Kirchhof Sep 28 '13 at 20:50
  • What debugging have you already done? Have you verified that `viewC.TilvalgteMedierArray` contains items? Have you checked the return values of each of the table's delegate methods? – Timothy Moose Sep 28 '13 at 20:50
  • @JörgKirchhof No, you're incorrect. `[NSMutableArray arrayWithArray:ValgteMedierarray]` is perfectly fine. Try it. – Timothy Moose Sep 28 '13 at 20:53
  • Of course it works - but it isn't good coding style. The API asked him to provide a `NSArray` and he provided a `NSMutableArray`. Calling `copy` forces `NSMutableArray` to provide an immutable version of itself. – Jörg Kirchhof Sep 28 '13 at 21:03
  • @TimothyMoose I have verified that `viewC.TilvalgteMedierArray`does contain items. I haven't done the delegate thing, though. What should i be looking for in that? – user1411094 Sep 28 '13 at 21:38
  • Verify that they're getting called and returning the expected values. For example, verify `numberOfRowsInSection` returns a non-zero value. – Timothy Moose Sep 28 '13 at 23:17

2 Answers2

0

Your ValgViewController is a Choice or Selection table view controller which allows the user to select which media types will be displayed within the Master table view controller.

You are wanting the Master table view controller to receive an array of choices selected by the user from the ValgViewController.

To do so requires a delegate protocol to be defined within the ValgViewController header, and for the Master table view controller to conform to that protocol.

As an example, you'd add the following within the ValgViewController header file:

@protocol ValgViewControllerDelegate <NSObject>
-(void)syncChoicesWithArray:(NSArray *)arrayToSync;
@end

@interface ValgViewController : UITableViewController
@property (nonatomic, assign) id <ValgViewControllerDelegate> delegate;
@property (nonatomic, strong) NSMutableArray *MedieValgListeArray, *ValgteMedierarray;
@end

Inside ValgViewController.m, the ValgViewController Done or Complete button will be connected to:

-(IBAction)actionSelectionCompleted:(id)sender {

    [self.delegate syncChoicesWithArray:ValgteMedierarray];
    // [mc.self sync:ValgteMedierarray];
    // [self dismissViewControllerAnimated:YES completion:nil];

}

Inside your MasterViewController.m file you'd have the MasterViewController adopt the protocol by adding:

@interface MasterViewController () <ValgViewControllerDelegate>{
    // no need for this if performing segues
    // ValgViewController *valgV;
}
@end

And then implement the delegate protocol by:

#pragma mark - ValgViewController Delegate methods

-(void)syncChoicesWithArray:(NSArray *)arrayToSync{
    // Dismiss the ValgViewController model from here
    [self dismissViewControllerAnimated:YES completion:nil];

    TilvalgteArray = arrayToSync;
    NSLog(@"valgte = %i", arrayToSync.count);
    NSLog(@"tilvalgte = %i", TilvalgteArray.count);
    NSLog(@"%@", [TilvalgteArray objectAtIndex:0]);
    [self.tableView reloadData];

}

You add the Master table view controller as the delegate of the ValgViewController within the prepareForSegue method:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString: @"showTilvalgteMedierScene"])
    {
        ValgViewController *vc = [segue destinationViewController];
        vc.delegate = self;
        vc.TilvalgteMedierArray = [NSMutableArray arrayWithArray:ValgteMedierarray];
    }
}
Phil Wilson
  • 888
  • 6
  • 8
  • Using this method, the app will just stack a lot of views on top of each other. I have to use the `[self dismissViewControllerAnimated:YES completion:nil];`, and as far as i know, i cannot do that in a segue from the storyboard. – user1411094 Sep 29 '13 at 10:25
  • Which is the Master table view controller, and which is the Detail table view controller? It is unclear. Are you trying to call the Detail controller from the dismissValgScene action method? If so, then why are you also dismissing the Master controller? – Phil Wilson Sep 29 '13 at 11:03
  • Please check my edit to main post. I have put up my project for you. – user1411094 Sep 29 '13 at 11:33
-1

I am betting that what is missing here is a proper protocol/delegate pattern.

You are referring to arrays and properties in another view that you should not be meddling with. Restrict each view to managing its own affairs.

Create a protocol, make the initial view the delegate, responding on changes to selection or on doneTouched. So essentially your selection view will only monitor and modify the selection and report back to those that are interested when changes are made or when it is finished. This view should not know or care what the delegate does from there.

Read up on delegates and protocols.

Delegates and Datasources

Protocols

Dean Davids
  • 4,174
  • 2
  • 30
  • 44