Skip to content

Instantly share code, notes, and snippets.

@akatreyt
Created May 7, 2014 23:53
Show Gist options
  • Save akatreyt/d49996b25d5f53d8116b to your computer and use it in GitHub Desktop.
Save akatreyt/d49996b25d5f53d8116b to your computer and use it in GitHub Desktop.
@implementation ThemeTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Themes.plist"];
NSMutableDictionary *savedThemes = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if(!savedThemes)
{
savedThemes = [[NSMutableDictionary alloc] init];
[savedThemes setObject:[[NSMutableArray alloc] init] forKey:@"savedThemes"];
}
[self setAllThemes:[NSMutableArray arrayWithArray:[savedThemes objectForKey:@"savedThemes"]]];
}
-(void)viewWillDisappear:(BOOL)animated
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Themes.plist"];
NSMutableDictionary *savedThemes = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if(!savedThemes)
{
savedThemes = [[NSMutableDictionary alloc] init];
[savedThemes setObject:[[NSMutableArray alloc] init] forKey:@"savedThemes"];
}
[savedThemes setObject:self.allThemes forKey:@"savedThemes"];
if(![savedThemes writeToFile:path atomically:YES])
{
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to save theme" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[error show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.allThemes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *theme = [self.allThemes objectAtIndex:indexPath.row];
[cell.textLabel setText:[theme valueForKey:@"Name"]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *theme = [self.allThemes objectAtIndex:indexPath.row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"loadNewTheme" object:theme];
[self.navigationController popToRootViewControllerAnimated:YES];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
NSDictionary *theme = [self.allThemes objectAtIndex:indexPath.row];
[self.allThemes removeObject:theme];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment