English 中文(简体)
UITableView with UISegmented control and checkmark accessory
原标题:

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

问题回答

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.




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签