// // ViewController.m // IBcells // // Created by Bogdan Michalchuk on 1/25/15. // Copyright (c) 2015 PDXRR. All rights reserved. // #import "ViewController.h" #import "BMOptionCell.h" #import "BMSettings.h" @interface ViewController () @property (nonatomic) BOOL expand; @end @implementation ViewController { NSArray *tableCellArray; BMSettings* settingSetup; } @synthesize selectedRowNumber; @synthesize rowExpanded; @synthesize objCellClicked; @synthesize expandedCell; - (instancetype) initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { settingSetup = [[BMSettings alloc] init]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.selectedRowNumber = -1; // Makes sure that row = nil doesnt expand self.rowExpanded = -1; 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 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.UDKey = [(BMSettings *)[tableCellArray objectAtIndex:indexPath.row] SKey]; [cell.cellSwitch addTarget:self action:@selector(switchUD:) forControlEvents:UIControlEventValueChanged]; // Display cell in the table cell BMOptionCell *setting = [tableCellArray objectAtIndex:indexPath.row]; cell.cellLabel.text = setting.name; 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"); cell.cellSwitch.on = YES; } else if ([[uDefaults stringForKey:cell.cellSwitch.UDKey] isEqualToString:@"OFF"]) { NSLog(@"State is OFF in this NSUserDefaults"); cell.cellSwitch.on = NO; } else { NSLog(@"Something went wrong with setting the switch"); } // Assign our own background image for the cell UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath]; UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:cell.cellView.frame]; [backgroundImage setImage:background]; [backgroundImage setContentMode:UIViewContentModeScaleAspectFill]; [cell.cellView sendSubviewToBack:backgroundImage]; // UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background]; // cellBackgroundView.image = background; // cell.backgroundView = cellBackgroundView; cell.cellView.backgroundColor = [UIColor colorWithPatternImage:background]; // // if (expandedCell == cell) { // cell.infoLabel.hidden = NO; // } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; selectedRowNumber = row; objCellClicked = [[self tableView] cellForRowAtIndexPath:indexPath]; [tableView beginUpdates]; [tableView endUpdates]; // [tableView deselectRowAtIndexPath:indexPath animated:YES]; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { BMOptionCell *cell = (BMOptionCell *)[tableView cellForRowAtIndexPath:indexPath]; [tableView beginUpdates]; [tableView endUpdates]; cell.infoLabel.hidden = YES; } -(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]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ BMOptionCell *cell = objCellClicked; if ([indexPath row] == selectedRowNumber) { if (!(selectedRowNumber == rowExpanded)) { self.rowExpanded = [indexPath row]; // self.expandedCell = objCellClicked; cell.infoLabel.hidden = NO; return 180; } if (selectedRowNumber == rowExpanded) { cell.infoLabel.hidden = YES; // expandedCell = nil; self.rowExpanded = -1; return 69; } } return 69; } //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // // NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]]; // UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0]; // CGSize labelSize = [cellText sizeWithFont:cellFont // constrainedToSize:constraintSize // lineBreakMode:UILineBreakModeWordWrap]; // return lableSize ; //} @end