textField appears twice in UITableView - uitableview

I have ageTextField property in my TableViewController. I add it to the first section cell's subciew but I don't know why it is shown in another section (#3) when I scroll down..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
switch (indexPath.section){
case 0:
{
[cell addSubview:self.ageTextField];
}
break;
case 1:
{
cell.textLabel.text = [screeningArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) //no choice
cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
else //yes choice
cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}
break;
case 2:
{
cell.textLabel.text = #"1 : ";
[cell addSubview:self.riskTextField];
}
break;
case 3:
{
cell.textLabel.text = [findingsArray objectAtIndex:indexPath.row];
cell.accessoryType = [[boolValuesArray objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}
break;
default:
break;
}
cell.textLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (textField == self.ageTextField)
return (newLength > 2) ? NO : YES;
else
return (newLength > 7) ? NO : YES;
}
Here is screenshot at the beginning..
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png
After scrolling down, notice how the placeholder of ageTextField was duplicated in Section3, cell3..
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png
When scrolling up again, the text of cell3 in section3 appears in the first cell..
dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png
That's weird and I couldn't figure out what happened! please help..

The contents of a table view cell must be removed if they are reused (i.e, if cell != nil). Also, the subviews should be added to the table cell's contentView not to the cell itself, so the subviews adjust appropriately when entering/exiting editing mode.
[cell.contentView addSubview:self.ageTextField];
[cell.contentView addSubview:self.riskTextField];
The following code prepares the cell for reuse.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
else
{
// Prepare cell for reuse
// Remove subviews from cell's contentView
for (UIView *view in cell.contentView.subviews)
{
// Remove only the appropriate views
if ([view isKindOfClass:[UITextField class]])
{
[view removeFromSuperview];
}
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = nil;
}

When the table gives you a reusable cell it doesn't care which section it was previously used in. Since you add a subview whether it's a new cell or one that previously had a subview added, it's quite possible to get more than one and of various kinds.

Related

Keep selected state of UITableViewCells when scrolling

I am trying to keep the selected state of multiple cells on a didSelectRowAtIndexPath method. I have an edit button that I've set up that loops through every cell to select each field on my UITableView.
Here is the code for the edit button on tap that selects all my rows.
- (IBAction)editButtonTapped:(id)sender {
for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
[self tableView:caseDataTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:i]];
}
}
}
When calling the didSelectRowAtIndexPath method, it does the following code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
OKOperatieNoteTableViewCell *cell = (OKOperatieNoteTableViewCell *)[self.caseDataTableView cellForRowAtIndexPath:indexPath];
cell.cellIndexPath = indexPath;
[cell hideLabelAndShowButtons];}
Incase you were wondering here is the hideLabelAndShowButtons method.
- (void)hideLabelAndShowButtons {
self.caseDataKeyLabel.hidden = NO;
if (!self.disabled) {
self.caseDataValueLabel.hidden = YES;
self.textField.hidden = NO;
if ([self.inputType isEqualToString:#"switcher"] || [self.inputType isEqualToString:#"multiselect"] || [self.inputType isEqualToString:#"picker"] || [self.inputType isEqualToString:#"DatePicker"] || [self.inputType isEqualToString:#"selectContact"]) {
self.button.hidden = NO;
}else {
self.button.hidden = YES;
}
}
self.caseDataDescriptionTextView.hidden = YES;}
Now at this point, I have all my rows selected. If I scroll down and then back up the selection of these rows is not there anymore. Now I'm aware when you go in and out of the view, the cellForRowAtIndexPath method recreates these cells. The following is my cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"caseData";
OKOperatieNoteTableViewCell * cell = [[OKOperatieNoteTableViewCell alloc]init];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (indexPath.row < _procedureVariables.count) {
if ([[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:#"key"] isEqualToString:#"Procedure"]) {
[cell setLabelsWithKey:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:#"key"] AndValue:[self.model valueForKey:#"var_procedureName"]];
}else {
[cell setLabelsWithKey:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:#"key"] AndValue:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:#"value"]];
}
OKProcedureTemplateVariablesModel *variableModel = _procedureVariables[indexPath.row];
cell.variable = variableModel.value;
[cell showLabelAndHideButtons];
cell.delegate = self;
[cell setUpCellType];
} else if (indexPath.row == _procedureVariables.count) {
NSString *text = [NSString stringWithFormat:#"%# \n\n %#", [_templateDictionary objectForKey:#"indicationText"], [_templateDictionary objectForKey:#"procedureText"] ];
[cell showDescription:text];
NSLog(#"cell.caseDataDescriptionTextView.font.fontName = %#", cell.caseDataDescriptionTextView.font.fontName);
}
cell.procedureID = _procedureID;
[tableView setContentInset:UIEdgeInsetsMake(1.0, 0.0, 0.0, 0.0)];
return cell;
}
I'm just trying to figure out how to keep the selected state of these cells once the cellForRowAtIndexPath method is called. Any suggestions are welcomed.
i tried to simulate your situation, created a customCell and saved the indexpaths of selectedRows in my custom selectedPaths mutable array(initialized in viewDidLoad).
After every click i removed or added related indexpath to my array.
it worked for my case. Hope it helps.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"caseData";
NOTableViewCell *cell = (NOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSLog(#"new cell created for row %d", (int)indexPath.row);
cell = [[[NSBundle mainBundle] loadNibNamed:#"NOTableViewCell" owner:self options:nil] objectAtIndex:0];
}
if ([selectedPaths indexOfObject:indexPath] != NSNotFound) // this cell is in selected state.
{
[cell.textLabel setText:#"This cell selected"];//selected state job.
return cell;
}
[cell.textLabel setText:[NSString stringWithFormat:#"%d", (int)indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedPaths indexOfObject:indexPath] != NSNotFound) {
[selectedPaths removeObject:indexPath];
}
else{
[selectedPaths addObject:indexPath];
}
//[tableView reloadData];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];//instead of reloading all just reload clicked cell.
}
You need to update the cell to selected and not selected explicitly in both directions in cellForRowAtIndexPath.
If not, the recycled cells will just show the value of the cell the cell was last used for until you change it.
While you are invoking the delegate method in order to call hideLabelAndShowButtons, you aren't telling the table view that you have selected the row;
- (IBAction)editButtonTapped:(id)sender {
for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
NSIndexPath *path=[NSIndexPath indexPathForRow:r inSection:i];
[caseDataTableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:caseDataTableView didSelectRowAtIndexPath:path];
}
}
}
Also, you aren't using the cell selection state in cellForRowAtIndexPath, so you probably need to change some code there too, but I am not sure what the relationship is between selected state and how you want to render the cell.

How to get value of uiTextField created programmatically in a uiTableView?

I'm a beginner in iOS and I try to get the value of my uiTextField that I created programmatically.
So my problem is that I create a form in an UITableViewController with a .json file. I have created my form, but I don't know how I can get the different values.
In my cellForRowAtIndexPath I created my different elements (I have textFields, textFields with picker and uiswitch). When the user have finished to fill out the form, he click on a "save" button and it is here that I want to get my values.
If someone can help me and put some code for explain .... :) Thanks.
some of my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *cellValue = [self getItemName:indexPath.section and:indexPath.row];
cell.textLabel.text = cellValue;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *itemType=[self getItemType:indexPath.section and:indexPath.row];
NSLog(#"%#",itemType);
if ([itemType isEqualToString:#"text"]) {
UITextField *Reference = [self createTextField];
[cell.contentView addSubview:Reference];
}else if ([itemType isEqualToString:#"checkbox"]) {
UIView *checkbox = [self createCheckbox:indexPath.section and:indexPath.row];
[cell.contentView addSubview:checkbox];
}else if ([itemType isEqualToString:#"picker"]) {
UITextField *Reference = [self createPicker:indexPath.section and:indexPath.row];
[cell.contentView addSubview:Reference];
}else if ([itemType isEqualToString:#"datePicker"]) {
UITextField *Reference = [self createDatePicker];
[cell.contentView addSubview:Reference];
}
return cell;
}
it is here that i create dynamically the elements. For example an uixtField :
-(UITextField*)createTextField{
UITextField *Reference = [[UITextField alloc] initWithFrame:CGRectMake(200 , 10, 200, 40)];
Reference.layer.borderColor=[[UIColor blackColor]CGColor];
Reference.layer.borderWidth=1.0f;
Reference.textAlignment=NSTextAlignmentCenter;
Reference.tag=_tag;
_tag++;
return Reference;
}
http://hpics.li/04b98ca
here it is an example of that i obtained with my dynamical generation.
Use:
[textField setTag:(integer value)]
when you creating fields in cellForRow... method.
Then use in saveMethod this:
[[tableView cellForIndexPath:indexPath] viewWithTag:(integer value)]
for get concrete field of concrete cell.
if ([itemType isEqualToString:#"text"]) {
UITextField *Reference = [self createTextField];
[Reference setTag:1];
[cell.contentView addSubview:Reference];
}
-(void)saveMethod
{
NSString *string = [[[self.tableView cellForRowAtIndexPath:indexPath] viewwithTag:1] text];
}
You can create elements in the method cellForRowAtIndexPath for your cells if after you want to get info from them, because it will be very difficult to get it.
The best way is creating your custom UITableViewCell with all elements you want and after, in the method didSelectRowAtIndexPath to get the info of your elements.
The way I get an indexPath from a view is by checking the superview until I get the parent cell, then using -indexPathForCell:.
- (UITableViewCell *)cellForView:(UIView *)view
{
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
view = view.superview;
}
return (UITableViewCell *)view;
}
- (NSIndexPath *)indexPathForView:(UIView *)view
{
return [self.tableView indexPathForCell:[self cellForView:view]];
}
Overall, this solution will run into problems. Cells do not persist in a table view. As soon as the cell is off the screen it is recycled by the table view.
This means you should not attempt to store data in a cell. A cell is used to present data and get user input, but never store data. Data is meant to be stored in the table data source.
In addition, it means you need to be careful when adding subviews to a cell. As the cell get recycled, the views you added in it's previous use will still be in the cell. You will need to have a cleanup section to -tableView:cellForRowAtIndexPath: to remove all the views you previously added.
My recommendation is to subclass UITableViewCell. You can either have 4 subclasses, one for each JSON type you have, or have 1 subclass which can be instantiated with in the 4 different ways. In my example, I have 1 subclass that can be instantiated 4 different ways.
Keep the different types of cells differentiated by using different reuse identifiers.
Use target/actions and delegates to get input from the controls on the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = nil;
NSString *itemType=[self getItemType:indexPath.section and:indexPath.row];
NSLog(#"%#",itemType);
if ([itemType isEqualToString:#"text"]) {
cellIdentifier = #"TextCell";
} else if ([itemType isEqualToString:#"checkbox"]) {
cellIdentifier = #"CheckboxCell";
} else if ([itemType isEqualToString:#"picker"]) {
cellIdentifier = #"PickerCell";
} else if ([itemType isEqualToString:#"datePicker"]) {
cellIdentifier = #"DatePickerCell";
}
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
if ([cellIdentifier isEqualToString:#"TextCell"]) {
cell = [MyTableViewCell textCellWithIdentifier:cellIdentifier];
} else if ([cellIdentifier isEqualToString:#"CheckboxCell"]) {
cell = [MyTableViewCell checkboxCellWithIdentifier:cellIdentifier];
} else if ([cellIdentifier isEqualToString:#"PickerCell"]) {
cell = [MyTableViewCell pickerCellWithIdentifier:cellIdentifier];
} else if ([cellIdentifier isEqualToString:#"DatePickerCell"]) {
cell = [MyTableViewCell datePickerCellWithIdentifier:cellIdentifier];
}
}
NSString *itemName = [self getItemName:indexPath.section and:indexPath.row];
cell.textLabel.text = itemName;
if ([itemType isEqualToString:#"text"]) {
NSString *itemValue = [self getItemValue:indexPath.section and:indexPath.row];
cell.textField.text = itemValue;
} else if ([itemType isEqualToString:#"checkbox"]) {
BOOL itemChecked = [self getItemChecked:indexPath.section and:indexPath.row];
cell.checkbox.selected = itemChecked;
} else if ([itemType isEqualToString:#"picker"]) {
NSString *itemValue = [self getItemValue:indexPath.section and:indexPath.row];
cell.pickerLabel = itemValue;
} else if ([itemType isEqualToString:#"datePicker"]) {
NSDate *itemDate = [self getItemDate:indexPath.section and:indexPath.row];
cell.datePickerLabel = itemDate;
}
return cell;
}
For my problem, I have create a NSMutableDictionaryin which i put my element like that :
key : "name"
value : "UITextField"
Like that i can get the textField everywhere in my class.
in .h file
create the dictionary
in .m
just put the element and when you want to go the text value do :
UITextField* myTextField= [dictionnary objectForKey:key];

Fetching cells having checkmark accessory

I am working with the table view. I added the checkmark Accessory on the first tap of the screen and removed this checkmark on the second tap. I added few ckeckmarks on the cells of table view. Now i want that the labels of the cells having the checkmarks should be displayed on the NSlog.
please help me out regarding this issue. Any help will be much appriciable.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 0)
{
return [celltitles count];
}
else
{
return 7;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView.tag == 0)
{
cell.textLabel.text = [celltitles objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
habitname = [[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];
habitname.placeholder = #"Habit Name";
habitname.delegate= self;
[cell addSubview:habitname];
}
else if (indexPath.row == 1)
{
timelbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 50)];
timelbl.text = #"OFF";
timelbl.textAlignment = NSTextAlignmentCenter;
[cell addSubview:timelbl];
timetitlelbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
timetitlelbl.text = #"Alert";
timetitlelbl.textAlignment = NSTextAlignmentCenter;
[cell addSubview:timetitlelbl];
toggleswitch = [[UISwitch alloc] initWithFrame:CGRectMake(250, 5, 50, 60)];
toggleswitch.on = NO;
[toggleswitch addTarget:self action:#selector(toggleSwitch) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
[cell addSubview:toggleswitch];
}
}
else if (tableView.tag == 1)
{
cell.textLabel.text = [daysarray objectAtIndex:indexPath.row];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1)
{
if (toggleswitch.on)
{
[self datepickershown:timelbl.text animated:YES];
if (self.datePickerIsShowing)
{
//[self datepickershown];
//[self hideDatePickerCell];
[dscptxt resignFirstResponder];
}
else
{
[dscptxt resignFirstResponder];
[self.timelbl resignFirstResponder];
[self showDatePickerCell];
}
}
else
{
timelbl.textColor = [UIColor grayColor];
}
}
else if (indexPath.row > 2 && indexPath.row <10)
{
NSLog(#"I am Selectin the row");
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
// NSLog(#"Selected Accessory Is %d",cell.accessoryType);
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
[self.tableview deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)savedata:(id)sender
{
}
Above is the code which i am using and on the save action i want the log.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[markedArray addObject:indexPath.row];
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
// NSLog(#"Selected Accessory Is %d",cell.accessoryType);
cell.accessoryType = UITableViewCellAccessoryNone;
[markedArray removeObjectAtIndex:indexPath.row];
}
}
- (void)savedata:(id)sender
{
//write here your code to save to database
for(int i=0; i<markedArray.count; i++)
NSLog#"saved labels are %#",[celltitles objectAtIndex:[markedArray objectAtIndex:i]];
}
Try this :)
You can get all selected indexPaths from below method.
NSArray *selectedIndexPaths = [self.Yourtableview indexPathsForSelectedRows];
after getting this selected index array. You can get title of cell using indexPath.row property.
You also can store selected objects (objects from where you show data in table) in additional array.
Just add/remove data-object to array in didSelectRow method and you will have table state data in any time later.
I don't recommend you to use the checkmark approach to find out the selected cells
Try this:
Create a NSMutableArray named 'selectedCells' (or something else). It can either be a property or an instance variable.
Instantiate it in -viewDidLoad method.
In bottom your -tableView:cellForRowAtIndexPath method add this code.
if([selectedCells containsObject:#(indexPath.row)]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
In your -tableView:didSelectRowForIndexPath: method call:
if([selectedCells containsObject:#(indexPath.row)]){
[selectedCells removeObject:#(indexPath.row)];
}else{
[selectedCells addObject:#(indexPath.row)];
}
Using this way you always have the info for the selected cell (or cells having checkmarks) in 'selectedCells'

UITableView not scrolling after i did this

I'm new to iOS developing. I have a UITableViewCell which has three buttons and they were not appearing to be pressed to touch. I implemented the following code. The pressing appeared to have been solved but the scrolling have been disabled. Here's my code to make buttons appear to be pressed on touch. The loop in the code is important otherwise the table will not behave the way I want it to. Here's my code for cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *textLabel = (UILabel *) [cell.contentView viewWithTag:5];
switch ([indexPath section]) {
case 0:
textLabel.text = [patientRoom objectAtIndex:[indexPath row]];
break;
case 1:
textLabel.text = [patientsBathroom objectAtIndex:[indexPath row]];
break;
}
if (shown){
if (selectedIndexPath.row == indexPath.row)
comments.hidden = NO;
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// Configure the cell...
for (id obj in cell.subviews){
if ([NSStringFromClass([obj class]) isEqualToString:#"UITableViewCellScrollView"]){
UIScrollView *scroll = (UIScrollView *) obj;
scroll.delaysContentTouches = NO;
break;
}
}
return cell;
}

Accessing a UISwitch from a custom table cell

I'm currently working on my second project which is focusing on CoreData and Custom Cells.
I have a custom cell with an image & label & switch, i'm trying to save the value of the switch to userdefaults when the value of the switch has changed. But i'm stuck with how to access each switch individually (there are 2 in the same section of my table view), so that when the switch is pressed the integer stored in userdefaults is instantly updated.
This is the code in the .m file concerning the custom cell (switchCell) apologises for any messy code etc, i'm pretty much 100% self taught without any advice/feedback on any mistakes i'm making.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Code for the first section, controlling which cells use the custom cell SwitchCell
if (indexPath.section == 0)
{
if(indexPath.row == 1 || indexPath.row == 2)
{
SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:#"SwitchCellIdentifier"];
cell = switchCell;
}
else
{
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"CellIdentifier"];
UISwitch *testSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = testSwitch;
}
}
}
// Each other section currently uses the standard cell type
else
{
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"CellIdentifier"];
}
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [settingsTableData objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Settings"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
SwitchCell *switchCell = (SwitchCell *)cell;
[[NSUserDefaults standardUserDefaults] synchronize];
// Integers to store the on/off state of each switch (2)
NSInteger capsValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"capitalsSwitchOn"];
NSInteger numbersValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"numbersSwitchOn"];
NSLog(#"Upon load capsValue equals : %d", capsValue);
NSLog(#"upon load numbersValue equals : %d", numbersValue);
// Setting individual cell values for attributes such as image and text
if (indexPath.section == 0)
{
if(indexPath.row == 1)
{
switchCell.switchCellLabel.text = cellValue;
switchCell.switchCellImage.image = [UIImage imageNamed:#"capitalsImage.jpg"];
// Set to true or false depending on what you want the default value to be.
//switchCell.switchCellSwitch.on = FALSE;
if (capsValue == 1) {
[switchCell.switchCellSwitch setOn:NO animated:YES];
} else
[switchCell.switchCellSwitch setOn:YES animated:YES];
}
else if (indexPath.row == 2)
{
switchCell.switchCellLabel.text = cellValue;
switchCell.switchCellImage.image = [UIImage imageNamed:#"capitalsImage.jpg"];
if (numbersValue == 1) {
[switchCell.switchCellSwitch setOn:NO animated:YES];
} else
[switchCell.switchCellSwitch setOn:YES animated:YES];
}
else
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else if (indexPath.section == 1)
{
if (indexPath.row == 0)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if (indexPath.row == 1)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else if (indexPath.section == 2)
{
if (indexPath.row == 0)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if (indexPath.row == 1 || indexPath.row == 2)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
Thanks in advance to any help!
I would add that switch in your subclassed cell and not in the viewController. Then in the cell subclass create a delegate, which would call a method like switchDidChangeValue in the viewController. There set your defaults.
Add an IBAction to your SwitchCell.
In the header file add:
- (IBAction)switchChanged: (UISwitch*)sender;
And in the .m file apply:
- (IBAction)switchChanged: (UISwitch*)sender {
BOOL value = sender.isOn;
// Do your Core Data work here
}
You might have to add a property to SwitchCell, so that it knows what database entry to create/change.
Then go into your Storyboard or .nib file, click on the UISwitch, open the last Tab in the object inspector. There you should be able to see Send Events -> Value Changed. Drag a connection from that to your SwitchCell, select your IBAction and you should be good to go.
So many codes :-) and I'm not suer if I understand your mind or not. But I think maybe you can define two properties in your viewController for two switches, switchOne and switchTwo , and you assign them in function
tableView:cellForRowAtIndexPath:
I always do this when I have independent controls in grouped tableView.
Still I have some question about your code:
Do you mean the second and third cell in your first section are cells with switches?
I think in your code
if(indexPath.row == 1 || indexPath.row == 2){
SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:#"SwitchCellIdentifier"];
cell = switchCell;
}
the cell will always be nil, because you never build a cell with identifier "SwitchCellIdentifier"
I think you build switch controls for other cells of your first section, so I totally have no idea what you want
It's 2:00 in the morning here in China, I worked all night long and I'm very tried, so maybe your code is right and I misunderstood, if so, please let me know.

Resources