0

I am a newbie in the world of Objective-C and iOS coding so I'll appreciate a little bit of help.

Here is my issue. I've got a UITableView with a UISegmentedControl. This one has 6 different segments which modify the content of the table by modifying an NSMutableArray. I managed to do that so I'm already pretty proud of me but still the newbie curse is back today. I want to implement checkmarks in order to select cells and pass the cells' data to another UITableView. The first issue is that I've got my checkmarks but I click on a different segment the data are updated but the checkmarks from the previous segment remain. How to address this problem.

Second what is the best way to pass data from all of the segment of this UITableView to another tableview by selecting the cells?

Here is my UITableViewController.h

@class MesExercicesViewController;

@protocol MesExercicesViewControllerDelegate <NSObject>

- (void) mesExercicesViewControllerDidCancel:
(MesExercicesViewController *) controller;

- (void) mesExercicesViewControllerDidSave:
(MesExercicesViewController *)controller;

@end



@interface MesExercicesViewController : UITableViewController {
NSMutableArray *exercicesList;
UISegmentedControl *segment;
}

- (IBAction)segmentChange;

@property (nonatomic, retain) IBOutlet UISegmentedControl *segment;

@property (nonatomic, weak) id <MesExercicesViewControllerDelegate> delegate;

- (IBAction)cancel:(id)sender;

- (IBAction)done:(id)sender;

@end

And here is the code of the UISegmentedControl in the UITableViewController.m

- (void)viewDidLoad {

[super viewDidLoad];
exercicesList = [NSMutableArray arrayWithObjects: 



@"A",@"A1",@"A2",@"A3",@"A4",@"A5",@"A6",@"A7", nil];
}


- (IBAction)segmentChange {
if (segment.selectedSegmentIndex == 0) {
    exercicesList = [NSMutableArray arrayWithObjects:@"A",@"A1",@"A2",@"A3",@"A4",@"A5",@"A6", nil];
    [[self tableView]reloadData]; 

} else if (segment.selectedSegmentIndex == 1) {
    exercicesList = [NSMutableArray arrayWithObjects:@"C",@"C1",@"C2",@"C3",@"C4", nil];
    [[self tableView] reloadData];

} else if (segment.selectedSegmentIndex == 2) {
    exercicesList = [NSMutableArray arrayWithObjects:@"E",@"E1",@"E2",@"E3",@"E4",@"E5",@"E6",@"E7",@"F",@"F1", nil];
    [[self tableView] reloadData];
} else if (segment.selectedSegmentIndex == 3) {
    exercicesList = [NSMutableArray arrayWithObjects:@"I",@"I1",@"I2",@"I3",@"I4",@"I5",@"I6",@"I7",@"I8",@"J", nil];
    [[self tableView] reloadData];

} else if (segment.selectedSegmentIndex == 4) {
    exercicesList = [NSMutableArray arrayWithObjects:@"L",@"M",@"M1",@"N",@"N1", nil];
    [[self tableView] reloadData];

} else if (segment.selectedSegmentIndex == 5) {
    exercicesList = [NSMutableArray arrayWithObjects:@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
    [[self tableView] reloadData];

}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    //reflect selection in data model
}else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    cell.accessoryType = UITableViewCellAccessoryNone;

    //reflect the deselection in data model
}

Thank you very much for your help in advance

1 Answers1

0

Firstly, in your cellForRowAtIndexPath set

cell.accessoryType = UITableViewCellAccessoryNone;

this way, whenever you reloadData on your tableView it will get rid of the check marks. You could do this when a new segment is tapped.

2nd Edit

Secondly, set up 2 complimentary methods - in didSelectRow and didDeselectRow. Create a NSMutableArray in your viewDidLoad and then add to it. So your didSelectRow would have:

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    [mutableArray addObject:[exercisesList objectAtIndex:indexPath.row]];
    }

and your didDeselectRow would have

  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    [mutableArray removeObject:[exercisesList objectAtIndex:indexPath.row]];
    }

and as far as the checkmarks go, have you implemented this?

[[self tableView] setEditing:YES animated:YES];

Then you would use this mutableArray to populate the new table.

1st EDIT

  • Here is some extra detail for how to set the delegate

So lets call the VC that has your original table - OriginalTableViewController and the VC with the new table that you want to populate from the mutableArray - NextTableViewController (best to stay away from the word 'new' at the start of any names...)

NextTableViewController.h - right after the # import < UIKit/UIKit.h>

@class NextTableViewController;

@protocol NextTableViewControllerDelegate  
 -(NSMutableArray*)sendThroughTheMutableArray;  
@end

declare your delegate

@property (nonatomic, assign) id delegate;

NextTableViewController.m

@synthesize delegate;

Then, you may be familiar with making calls to self like - [self getThatArray]; its the same with a delegate, but you just make the call to delegate instead of self

This is assuming you've declared the myTablePopulatingArray in your h file:

if(delegate != nil)
{
myTablePopulatingArray = [[NSMutableArray Alloc] initWithArray: [delegate sendThroughTheMutableArray]];
}

Basically to this point we have just set up the delegate. We are saying, this is what I need. Who's up for it? Who will volunteer for this job. We put the if(delegate != nil) as a safety - but you are the one who will make sure there is a delegate, so you probably don't really need it

Now for the delegate itself - you only need 2 things:

  1. in your OriginalTableViewController.h file

#import "NextTableViewController.h"

@class DetailViewController; OriginalTableViewController

  1. in your OriginalTableViewController.m file you must put the method that you declared earlier

    -(NSMutableArray*)sendThroughTheMutableArray
    {
    return mutableArray;
    }

so this mutableArray is now ready to populate your tableView.
Shane Rayner
  • 672
  • 6
  • 8
  • Ok the accessory issue is solved thanks to you but now I'm struggling to access the newly created NSMutableArray (mutableArray) from my other UITableViewController. I know that one of the best way is to create a delegate but I'm investigating right now...if you have any hint, I would appreciate. – Yoann Lopez Apr 20 '12 at 19:32
  • So I've just added a heap more detail to my answer. I've put how to set a delegate as well. Let me know how you go. – Shane Rayner Apr 21 '12 at 00:29
  • Thanks a lot, I'll try this tomorrow as today I'm kinda busy. Don't worry for the formatting, I'm also new to SO :-) Thanks a lot for your help Shane. I do appreciate a lot. – Yoann Lopez Apr 21 '12 at 20:59
  • Ok I edited on top of your answer to let you know what I exactly did in order to see what's wrong in my code. Thanks again for allotting some of your time to my problem. – Yoann Lopez Apr 22 '12 at 21:40
  • I've added a '2nd Edit'. Im not entirely sure what the problem is. I have just put more detail regarding didSelectRow and didDeselect row. They are 2 separate methods. I may have misunderstood what you were doing. There are some things that you put in there that shouldn't be necessary if you have set the table editing property to YES. – Shane Rayner Apr 22 '12 at 23:26
  • Apparently my Edit to your answer hasn't been reviewed because I can't see it right now. Probably tomorrow... You'll see more details about what I did. And where I probably messed up. – Yoann Lopez Apr 23 '12 at 01:26