Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ViewController.h"
- #import "BMOptionCell.h"
- #import "BMSettings.h"
- @interface ViewController ()
- @end
- @implementation ViewController {
- NSArray *tableCellArray;
- BMSettings* settingSetup;
- }
- - (instancetype) initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- settingSetup = [[BMSettings alloc] init];
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- tableCellArray = [settingSetup getArray];
- self.tableView.dataSource = self;
- self.tableView.delegate = self;
- // Assign our own backgroud for the view
- self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
- self.tableView.backgroundColor = [UIColor clearColor];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- // Return the number of sections.
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- return tableCellArray.count;
- }
- - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
- NSInteger rowIndex = indexPath.row;
- UIImage *background = nil;
- if (rowIndex == 0) {
- background = [UIImage imageNamed:@"cell_top.png"];
- } else if (rowIndex == rowCount - 1) {
- background = [UIImage imageNamed:@"cell_bottom.png"];
- } else {
- background = [UIImage imageNamed:@"cell_middle.png"];
- }
- return background;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- BMOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- cell.cellSwitch.tag = indexPath.row;
- cell.cellSwitch.UDKey = [(BMSettings *)[tableCellArray objectAtIndex:indexPath.row] SKey];
- [cell.cellSwitch addTarget:self action:@selector(switchUD:) forControlEvents:UIControlEventValueChanged];
- // Display setting in the table cell
- BMOptionCell *setting = [tableCellArray objectAtIndex:indexPath.row];
- UILabel *settingNameLabel = (UILabel *)[cell viewWithTag:101];
- settingNameLabel.text = setting.name;
- BMCustomSwitch *switchState = (BMCustomSwitch *) [cell viewWithTag:102];
- NSUserDefaults *uDefaults = [NSUserDefaults standardUserDefaults];
- NSLog(@"%@ is the cell.cellSwitch.UDKey String", cell.cellSwitch.UDKey);
- if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"ON"]) {
- NSLog(@"State is ON in this NSUserDefaults");
- // [switchState setOn:YES animated:YES];
- switchState.on = YES;
- }
- else if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"OFF"]) {
- NSLog(@"State is OFF in this NSUserDefaults");
- // [switchState setOn:NO animated:YES];
- switchState.on = NO;
- }
- else if (![uDefaults stringForKey:cell.cellSwitch.UDKey]) {
- NSLog(@"There is no NSUserDefault at this key");
- }
- else {
- NSLog(@"Something went wrong with setting the switch");
- }
- // Assign our own background image for the cell
- UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
- UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
- cellBackgroundView.image = background;
- cell.backgroundView = cellBackgroundView;
- return cell;
- }
- //-(void)switchChanged:(id)sender {
- // UISwitch *senderSwitch = (UISwitch *)sender;
- // NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
- // if (senderSwitch.tag == 0) {
- // if (senderSwitch.on == 1) {
- // [uD setObject:@"ON" forKey:@"Call Alert"];
- // NSLog(@"Switch 1 ON");
- // }
- // else if (senderSwitch.on == 0) {
- // [uD setObject:@"OFF" forKey:@"Call Alert"];
- // NSLog(@"Switch 1 OFF");
- // }
- // }
- // if (senderSwitch.tag == 1) {
- // if (senderSwitch.on == 1) {
- // [uD setObject:@"ON" forKey:@"AutoPlay"];
- // NSLog(@"Switch 2 ON");
- // }
- // else if (senderSwitch.on == 0) {
- // [uD setObject:@"OFF" forKey:@"AutoPlay"];
- // NSLog(@"Switch 2 OFF");
- // }
- // }
- // if (senderSwitch.tag == 2) {
- // if (senderSwitch.on == 1) {
- // [uD setObject:@"ON" forKey:@"Orientation"];
- // NSLog(@"Switch 3 ON");
- // }
- // else if (senderSwitch.on == 0) {
- // [uD setObject:@"OFF" forKey:@"Orientation"];
- // NSLog(@"Switch 3 OFF");
- // }
- // }
- // [uD synchronize];
- //}
- -(void)switchUD:(id)sender {
- BMCustomSwitch *senderSwitch = (BMCustomSwitch *)sender;
- NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
- if (senderSwitch.on == 1) {
- [uD setObject:@"ON" forKey:senderSwitch.UDKey];
- NSLog(@"Switch %@ ON", senderSwitch.UDKey);
- }
- else if (senderSwitch.on == 0) {
- [uD setObject:@"OFF" forKey:senderSwitch.UDKey];
- NSLog(@"Switch %@ OFF", senderSwitch.UDKey);
- }
- [uD synchronize];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment