Advertisement
redribben

vc

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