redribben

more progress

Jan 29th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "ViewController.h"
  2. #import "BMOptionCell.h"
  3. #import "BMSettings.h"
  4.  
  5. @interface ViewController ()
  6.  
  7. @end
  8.  
  9. @implementation ViewController {
  10.     NSArray *tableCellArray;
  11.     BMSettings* settingSetup;
  12. }
  13.  
  14. - (instancetype) initWithCoder:(NSCoder *)aDecoder {
  15.     if (self = [super initWithCoder:aDecoder]) {
  16.         settingSetup = [[BMSettings alloc] init];
  17.     }
  18.     return self;
  19. }
  20.  
  21. - (void)viewDidLoad {
  22.     [super viewDidLoad];
  23.    
  24.     tableCellArray =  [settingSetup getArray];
  25.    
  26.     self.tableView.dataSource = self;
  27.     self.tableView.delegate = self;
  28.  
  29.     // Assign our own backgroud for the view
  30.     self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
  31.     self.tableView.backgroundColor = [UIColor clearColor];
  32. }
  33.  
  34. - (void)didReceiveMemoryWarning {
  35.     [super didReceiveMemoryWarning];
  36.     // Dispose of any resources that can be recreated.
  37. }
  38.  
  39. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  40. {
  41.     // Return the number of sections.
  42.     return 1;
  43. }
  44.  
  45.  
  46. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  47. {
  48.     // Return the number of rows in the section.
  49.     return tableCellArray.count;
  50. }
  51.  
  52. - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
  53. {
  54.     NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
  55.     NSInteger rowIndex = indexPath.row;
  56.     UIImage *background = nil;
  57.    
  58.     if (rowIndex == 0) {
  59.         background = [UIImage imageNamed:@"cell_top.png"];
  60.     } else if (rowIndex == rowCount - 1) {
  61.         background = [UIImage imageNamed:@"cell_bottom.png"];
  62.     } else {
  63.         background = [UIImage imageNamed:@"cell_middle.png"];
  64.     }
  65.    
  66.     return background;
  67. }
  68.  
  69.  
  70. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72.     static NSString *CellIdentifier = @"Cell";
  73.     BMOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  74.    
  75.     cell.cellSwitch.tag = indexPath.row;
  76.    
  77.     cell.cellSwitch.UDKey = [(BMSettings *)[tableCellArray objectAtIndex:indexPath.row] SKey];
  78.    
  79.     [cell.cellSwitch addTarget:self action:@selector(switchUD:) forControlEvents:UIControlEventValueChanged];
  80.    
  81.     // Display setting in the table cell
  82.     BMOptionCell *setting = [tableCellArray objectAtIndex:indexPath.row];
  83.    
  84.     UILabel *settingNameLabel = (UILabel *)[cell viewWithTag:101];
  85.     settingNameLabel.text = setting.name;
  86.    
  87.     BMCustomSwitch *switchState = (BMCustomSwitch *) [cell viewWithTag:102];
  88.     NSUserDefaults *uDefaults = [NSUserDefaults standardUserDefaults];
  89.    
  90.     NSLog(@"%@ is the cell.cellSwitch.UDKey String", cell.cellSwitch.UDKey);
  91.     if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"ON"]) {
  92.         NSLog(@"State is ON in this NSUserDefaults");
  93. //        [switchState setOn:YES animated:YES];
  94.         switchState.on = YES;
  95.     }
  96.     else if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"OFF"]) {
  97.         NSLog(@"State is OFF in this NSUserDefaults");
  98. //        [switchState setOn:NO animated:YES];
  99.         switchState.on = NO;
  100.     }
  101.     else if (![uDefaults stringForKey:cell.cellSwitch.UDKey]) {
  102.         NSLog(@"There is no NSUserDefault at this key");
  103.     }
  104.     else {
  105.         NSLog(@"Something went wrong with setting the switch");
  106.     }
  107.    
  108.     // Assign our own background image for the cell
  109.     UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
  110.    
  111.     UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
  112.     cellBackgroundView.image = background;
  113.     cell.backgroundView = cellBackgroundView;
  114.    
  115.     return cell;
  116. }
  117.  
  118. //-(void)switchChanged:(id)sender {
  119. //    UISwitch *senderSwitch = (UISwitch *)sender;
  120. //    NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
  121. //    if (senderSwitch.tag == 0) {
  122. //        if (senderSwitch.on == 1) {
  123. //            [uD setObject:@"ON" forKey:@"Call Alert"];
  124. //            NSLog(@"Switch 1 ON");
  125. //        }
  126. //        else if (senderSwitch.on == 0) {
  127. //            [uD setObject:@"OFF" forKey:@"Call Alert"];
  128. //            NSLog(@"Switch 1 OFF");
  129. //        }
  130. //    }
  131. //    if (senderSwitch.tag == 1) {
  132. //        if (senderSwitch.on == 1) {
  133. //            [uD setObject:@"ON" forKey:@"AutoPlay"];
  134. //            NSLog(@"Switch 2 ON");
  135. //        }
  136. //        else if (senderSwitch.on == 0) {
  137. //            [uD setObject:@"OFF" forKey:@"AutoPlay"];
  138. //            NSLog(@"Switch 2 OFF");
  139. //        }
  140. //    }
  141. //    if (senderSwitch.tag == 2) {
  142. //        if (senderSwitch.on == 1) {
  143. //            [uD setObject:@"ON" forKey:@"Orientation"];
  144. //            NSLog(@"Switch 3 ON");
  145. //        }
  146. //        else if (senderSwitch.on == 0) {
  147. //            [uD setObject:@"OFF" forKey:@"Orientation"];
  148. //            NSLog(@"Switch 3 OFF");
  149. //        }
  150. //    }
  151. //    [uD synchronize];
  152. //}
  153.  
  154. -(void)switchUD:(id)sender {
  155.     BMCustomSwitch *senderSwitch = (BMCustomSwitch *)sender;
  156.     NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
  157.         if (senderSwitch.on == 1) {
  158.             [uD setObject:@"ON" forKey:senderSwitch.UDKey];
  159.             NSLog(@"Switch %@ ON", senderSwitch.UDKey);
  160.         }
  161.         else if (senderSwitch.on == 0) {
  162.             [uD setObject:@"OFF" forKey:senderSwitch.UDKey];
  163.             NSLog(@"Switch %@ OFF", senderSwitch.UDKey);
  164.         }
  165.     [uD synchronize];
  166. }
  167.  
  168. @end
Advertisement
Add Comment
Please, Sign In to add comment