redribben

for switch viewing

Jan 31st, 2015
228
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. @property (nonatomic) BOOL expand;
  16.  
  17. @end
  18.  
  19.  
  20. @implementation ViewController {
  21.     NSArray *tableCellArray;
  22.     BMSettings* settingSetup;
  23. }
  24.  
  25. @synthesize selectedRowNumber;
  26. @synthesize rowExpanded;
  27. @synthesize objCellClicked;
  28. @synthesize expandedCell;
  29.  
  30. - (instancetype) initWithCoder:(NSCoder *)aDecoder {
  31.     if (self = [super initWithCoder:aDecoder]) {
  32.         settingSetup = [[BMSettings alloc] init];
  33.     }
  34.     return self;
  35. }
  36.  
  37. - (void)viewDidLoad {
  38.     [super viewDidLoad];
  39.     self.selectedRowNumber = -1;  // Makes sure that row = nil doesnt expand
  40.     self.rowExpanded = -1;
  41.     tableCellArray =  [settingSetup getArray];
  42.    
  43.     self.tableView.dataSource = self;
  44.     self.tableView.delegate = self;
  45.  
  46.     // Assign our own backgroud for the view
  47.     self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
  48.     self.tableView.backgroundColor = [UIColor clearColor];
  49. }
  50.  
  51. - (void)didReceiveMemoryWarning {
  52.     [super didReceiveMemoryWarning];
  53.     // Dispose of any resources that can be recreated.
  54. }
  55.  
  56. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  57.     return 1;
  58. }
  59.  
  60. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  61.     // Return the number of rows in the section.
  62.     return tableCellArray.count;
  63. }
  64.  
  65. - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath {
  66.     NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
  67.     NSInteger rowIndex = indexPath.row;
  68.     UIImage *background = nil;
  69.    
  70.     if (rowIndex == 0) {
  71.         background = [UIImage imageNamed:@"cell_top.png"];
  72.     } else if (rowIndex == rowCount - 1) {
  73.         background = [UIImage imageNamed:@"cell_bottom.png"];
  74.     } else {
  75.         background = [UIImage imageNamed:@"cell_middle.png"];
  76.     }
  77.    
  78.     return background;
  79. }
  80.  
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  82.     static NSString *CellIdentifier = @"Cell";
  83.     BMOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  84.    
  85.     cell.cellSwitch.UDKey = [(BMSettings *)[tableCellArray objectAtIndex:indexPath.row] SKey];
  86.    
  87.     [cell.cellSwitch addTarget:self action:@selector(switchUD:) forControlEvents:UIControlEventValueChanged];
  88.    
  89.     // Display cell in the table cell
  90.     BMOptionCell *setting = [tableCellArray objectAtIndex:indexPath.row];
  91.    
  92.     cell.cellLabel.text = setting.name;
  93.  
  94.     NSUserDefaults *uDefaults = [NSUserDefaults standardUserDefaults];
  95.     NSLog(@"%@ is the cell.cellSwitch.UDKey String", cell.cellSwitch.UDKey);
  96.     if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"ON"]) {
  97.         NSLog(@"State is ON in this NSUserDefaults");
  98.         cell.cellSwitch.on = YES;
  99.     }
  100.     else if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"OFF"]) {
  101.         NSLog(@"State is OFF in this NSUserDefaults");
  102.         cell.cellSwitch.on = NO;
  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 *backgroundImage = [[UIImageView alloc] initWithFrame:cell.cellView.frame];
  112.     [backgroundImage setImage:background];
  113.     [backgroundImage setContentMode:UIViewContentModeScaleAspectFill];
  114.    
  115.     [cell.cellView sendSubviewToBack:backgroundImage];
  116.    
  117. //    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
  118. //    cellBackgroundView.image = background;
  119. //    cell.backgroundView = cellBackgroundView;
  120.    
  121.     cell.cellView.backgroundColor = [UIColor colorWithPatternImage:background];
  122. //    
  123. //    if (expandedCell == cell) {
  124. //        cell.infoLabel.hidden = NO;
  125. //    }
  126.    
  127.     return cell;
  128. }
  129.  
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  131.     NSInteger row = [indexPath row];
  132.     selectedRowNumber = row;
  133.     objCellClicked = [[self tableView] cellForRowAtIndexPath:indexPath];
  134.     [tableView beginUpdates];
  135.     [tableView endUpdates];
  136.  //   [tableView deselectRowAtIndexPath:indexPath animated:YES];
  137. }
  138.  
  139. -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  140.     BMOptionCell *cell = (BMOptionCell *)[tableView cellForRowAtIndexPath:indexPath];
  141.     [tableView beginUpdates];
  142.     [tableView endUpdates];
  143.     cell.infoLabel.hidden = YES;
  144. }
  145.  
  146. -(void)switchUD:(id)sender {
  147.     BMCustomSwitch *senderSwitch = (BMCustomSwitch *)sender;
  148.     NSUserDefaults *uD = [NSUserDefaults standardUserDefaults];
  149.         if (senderSwitch.on == 1) {
  150.             [uD setObject:@"ON" forKey:senderSwitch.UDKey];
  151.             NSLog(@"Switch %@ ON", senderSwitch.UDKey);
  152.         }
  153.         else if (senderSwitch.on == 0) {
  154.             [uD setObject:@"OFF" forKey:senderSwitch.UDKey];
  155.             NSLog(@"Switch %@ OFF", senderSwitch.UDKey);
  156.         }
  157.     [uD synchronize];
  158. }
  159.  
  160. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  161.     BMOptionCell *cell = objCellClicked;
  162.     if ([indexPath row] == selectedRowNumber) {
  163.         if (!(selectedRowNumber == rowExpanded)) {
  164.             self.rowExpanded = [indexPath row];
  165. //            self.expandedCell = objCellClicked;
  166.             cell.infoLabel.hidden = NO;
  167.             return  180;
  168.         }
  169.         if (selectedRowNumber == rowExpanded) {
  170.             cell.infoLabel.hidden = YES;
  171. //            expandedCell = nil;
  172.             self.rowExpanded = -1;
  173.             return 69;
  174.         }
  175.     }
  176.     return 69;
  177. }
  178.  
  179. //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  180. //    
  181. //    NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
  182. //    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
  183. //    CGSize labelSize = [cellText sizeWithFont:cellFont
  184. //                            constrainedToSize:constraintSize
  185. //                                lineBreakMode:UILineBreakModeWordWrap];
  186. //    return lableSize ;
  187. //}
  188.  
  189. @end
Advertisement
Add Comment
Please, Sign In to add comment