Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-21-2011, 10:35 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
WeaselPig is on a distinguished road
Default Saving selected table rows

Hi Guys

I'm building an app which will allow people to select one or more rows from a table view which shows the day of the week by using a check mark.

I would then like a separate view to show a label which lists which days have been selected from the table view.

I have set up my table view and the other view with no issues but I am having real problems saving which selections the user has chosen.

Any help would be greatly appreciated.

Thanks
WeaselPig is offline   Reply With Quote
Old 09-21-2011, 12:17 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

Well this is just me thinking out loud but one thing you could do is in the didSelectrow method of the table view you have have an Array that add's the text of each cell to the array when it's selected and then pass that array over to the new view to populate the label. Without any code example of what your working with I don't know if this would fully work or not.
mavrik5150 is offline   Reply With Quote
Old 09-23-2011, 05:59 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
WeaselPig is on a distinguished road
Default

Quote:
Originally Posted by mavrik5150 View Post
Well this is just me thinking out loud but one thing you could do is in the didSelectrow method of the table view you have have an Array that add's the text of each cell to the array when it's selected and then pass that array over to the new view to populate the label. Without any code example of what your working with I don't know if this would fully work or not.
This is my code so far, the good news is that by using NSUserdefaults my selected days are saved and I am able to set up the label on the other view.
The bad news is that when I go back on to the select a day table view the user default loads but is then wiped clear.

I know that this has something to do with the cellForRowAtIndexPath method but I am unable to figure out how to make the chekmarks persist based on the saved data in NSUserdefaults.

Can anyone help please?

Code:
#import "DayView.h"

@implementation DayView

@synthesize sourceArray;
@synthesize selectedArray;



- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
        [[self navigationItem] setTitle:@"Days"];
        
        [[self tableView] setBackgroundColor:[UIColor clearColor]]; 
        
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    // create a standardUserDefaults variable
    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    
    // Convert array to string
    NSString *time = [[selectedArray valueForKey:@"description"] componentsJoinedByString:@","];
    
    // saving an NSString
    [standardUserDefaults setObject:time forKey:@"string"];
    
    NSLog(@"Disapear: %@", time);
}



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // create a standardUserDefaults variable
    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    
    // getting an NSString object
    NSString *myString = [standardUserDefaults stringForKey:@"string"];
    
    NSLog(@"Appear: %@", myString);
    

    NSMutableArray * tempArray = [[NSMutableArray alloc] init];
	[self setSelectedArray:tempArray];
	[tempArray release];
	
	NSArray * tempSource = [[NSArray alloc] initWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday",nil];
	[self setSourceArray:tempSource];
	[tempSource release];
    
	[self.tableView reloadData];
}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return [self.sourceArray count];;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    

    NSString *time = [sourceArray objectAtIndex:indexPath.row];
    cell.textLabel.text = time;
    
    
	if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]])

    {
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
	}
	else
	{
		[cell setAccessoryType:UITableViewCellAccessoryNone];
	}
    
   
    NSLog(@"Selected Days: %@", selectedArray);

    
    return cell; 
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Which times you are available?"; 
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]]){
		[self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:[self.sourceArray objectAtIndex:indexPath.row]]];
	}
	else
	{
		[self.selectedArray addObject:[self.sourceArray objectAtIndex:indexPath.row]];
	}
	
	[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
WeaselPig is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 395
14 members and 381 guests
7twenty7, eski, EvilElf, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0