TableView isn't updating data even though it recognizes input? - iphone

So I have 5 rows and on selection they pass an integer with the row number to my second view controller.
Each number has its own array with items and should then return the amount of items for the row specified.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (radInt == 0) {
cell.textLabel.text = #"LM";
NSLog(#"My return should have been LM");
}
if (radInt == 1) {
cell.textLabel.text = #"Restauranger";
NSLog(#"My return should have been Rest");
}
if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:#"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
This is my code just to test it out and it doesn't work. NSLOG works correctly but the data simply wount update... What have I done wrong? It Nslogs LM every time but it also has 1 in the log which is the selected row (radInt).
New approach
static NSString *CellIdentifier = #"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell = [[MasterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (radInt == 0) {
cell.textLabel.text = #"LM";
NSLog(#"My return should have been LM");
}
else if (radInt == 1) {
cell.textLabel.text = #"Restauranger";
NSLog(#"My return should have been Rest");
}
else if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
else if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = #"Noes";
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:#"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
The view before ----
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *enjoy = [[SecondViewController alloc] init];
if ([[array objectAtIndex:indexPath.row] isEqual:#"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 0;
NSLog(#"0");
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 1;
NSLog(#"1");
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Annat"]) {
enjoy.radInt = 3;
}
[self performSegueWithIdentifier:#"main" sender:self];
}

You don't actually need to implement didSelectRowAtIndexPath: if you connect the segue in the storyboard from the cell to the next controller. What you need to implement is prepareForSegue:. Your code should look something like this:
- (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender
{
NSInteger row = [self.tableView indexPathForSelectedRow].row;
SecondViewController *enjoy = segue.destinationViewController;
if ([[array objectAtIndex:row] isEqual:#"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 0;
NSLog(#"0");
}
if ([[array objectAtIndex:row] isEqual:#"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 1;
NSLog(#"1");
}
if ([[array objectAtIndex:row] isEqual:#"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:row] isEqual:#"Annat"]) {
enjoy.radInt = 3;
}
}

Related

having error with custom checkboxes in uitableview cells

im getting the error: * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'*
I have 2 sections and I am trying to make it so when you click the checkbox of a cell in one of the sections, it goes to the other section (ex: section 1->section 2)
here is some relevant code of mine:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"UITableViewCell"];
if([indexPath section] == 0){
cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"checkboxtry2.png"];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[[completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"checkboxtry2selected.png"];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handlechecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}
-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
if (tappedIndexPath.section == 0) {
[completedArray addObject:[taskArray objectAtIndex:tappedIndexPath.row]];
[taskArray removeObject:[taskArray objectAtIndex:tappedIndexPath.row]];
}
else {
[taskArray addObject:[completedArray objectAtIndex:tappedIndexPath.row]];
[completedArray removeObject:[completedArray objectAtIndex:tappedIndexPath.row]];
}
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
I have two arrays: taskArray which handles objects in section 0 and completedArray which handles objects in section 1.
---EDIT---
Here is what I have now:
TableViewController.h
#interface ToDoTableViewController : UITableViewController <Properties2ViewControllerDelegate, UITableViewDelegate, SettingsViewControllerDelegate>
#property (strong, nonatomic) NSMutableArray *taskArray;
#property (strong, nonatomic) NSMutableArray *completedArray;
#property (strong, nonatomic) NSMutableArray *holdViewsArray;
-(IBAction)addCell:(id)sender;
-(void)buttonPressed:(id)sender;
-(void)handlechecking:(UITapGestureRecognizer *)t;
TableViewController.m
-(void) viewDidLoad{
[self.tableView setDelegate:self];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
taskArray = [[NSMutableArray alloc] init];
completedArray = [[NSMutableArray alloc]init];
holdViewsArray = [[NSMutableArray alloc]init];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"UITableViewCell"];
NSString *detailText = [NSString stringWithFormat:#"%.0f", [[taskArray objectAtIndex:[indexPath row]] timeInterval]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
cell.textLabel.textColor = baseColor;
[[cell detailTextLabel] setText:detailText];
[[cell detailTextLabel] setFont:[UIFont fontWithName:#"Avenir-Black" size:12]];
[[cell textLabel] setFont:[UIFont fontWithName:#"AvenirNext-DemiBold" size:16]];
if([indexPath section] == 0){
cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"unchecked.png"];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[[completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"checked.png"];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handlechecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}
-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
NSIndexPath *newIndexPath = nil;
if (tappedIndexPath.section == 0) {
NSUInteger newRowIndex = self.completedArray.count;
[self.completedArray addObject:[self.taskArray objectAtIndex:tappedIndexPath.row]];
[self.taskArray removeObject:[self.taskArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:1];
} else {
NSUInteger newRowIndex = self.taskArray.count;
[self.taskArray addObject:[self.completedArray objectAtIndex:tappedIndexPath.row]];
[self.completedArray removeObject:[self.completedArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView deleteRowsAtIndexPaths:#[tappedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger num = 0;
if (section == 0) {
num = self.taskArray.count;
} else {
num = self.completedArray.count;
}
return num;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
Tasks *taskToMove = [taskArray objectAtIndex:[sourceIndexPath row]];
if (sourceIndexPath.row > destinationIndexPath.row) {
[taskArray insertObject:taskToMove atIndex:destinationIndexPath.row];
[taskArray removeObjectAtIndex:(sourceIndexPath.row + 1)];
}
else if (sourceIndexPath.row < destinationIndexPath.row) {
[taskArray insertObject:taskToMove atIndex:(destinationIndexPath.row + 1)];
[taskArray removeObjectAtIndex:(sourceIndexPath.row)];
}
}
-(IBAction)addCell:(id)sender{
Properties2ViewController *pvc = [[Properties2ViewController alloc]init];
[pvc setDelegate:self];
[self presentViewController:pvc animated:YES completion:NULL];
[pvc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
}
-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{
if (![[t taskName] isEqual: #""]) {
[taskArray addObject:t];
}
[self.tableView reloadData];
}
Properties2ViewController.m
-(IBAction)dismiss:(id)sender{
testTask = [[Tasks alloc]init];
testTask.taskName = taskName.text;
testTask.timeInterval = datePicker.countDownDuration;
testTask.dateCreated = [NSDate date];
if ([self.delegate respondsToSelector:#selector (properties2ViewControllerDidEnterPropertiesSuccesfully:)]){
[self.delegate properties2ViewControllerDidEnterPropertiesSuccesfully:testTask];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
Properties2viewcontroller is a modal controller that adds a Task object to the taskArray.
You are trying to reload rows but what you actually want to do is delete row from section 0 and add it to section 1 or vise versa. So in handlechecking method you must write something like this:
-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
NSIndexPath *newIndexPath = nil;
if (tappedIndexPath.section == 0) {
NSUInteger newRowIndex = self.sectionTwoArr.count;
[self.sectionTwoArr addObject:[self.sectionOneArr objectAtIndex:tappedIndexPath.row]];
[self.sectionOneArr removeObject:[self.sectionOneArr objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowindex inSection:1];
} else {
NSUInteger newRowIndex = self.sectionOneArr.count;
[self.sectionOneArr addObject:[self.sectionTwoArr objectAtIndex:tappedIndexPath.row]];
[self.sectionTwoArr removeObject:[self.sectionTwoArr objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowindex inSection:0];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView deleteRowsAtIndexPaths:#[tappedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
Edit
Full implementation of other methods
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sectionOneArr = [#[#"ololo", #"dsd", #"dsdfsf"] mutableCopy];
self.sectionTwoArr = [#[#"ototo",#"dd", #"sdfsdfsd"] mutableCopy];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger num = 0;
if (section == 0) {
num = self.sectionOneArr.count;
} else {
num = self.sectionTwoArr.count;
}
return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"UITableViewCell"];
if([indexPath section] == 0){
cell.textLabel.text = [[self.sectionOneArr objectAtIndex:[indexPath row]] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"unchecked.jpeg"];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[self.sectionTwoArr objectAtIndex:[indexPath row]] uppercaseString];
cell.imageView.image = [UIImage imageNamed:#"checked.jpeg"];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handlechecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString * num = nil;
if (section == 0) {
num = #"One";
} else {
num = #"Two";
}
return num;
}
Your code looks correct but I would venture a guess that the problem is in your tableView:numberOfRowsInSection: method. Are you returning the proper count of rows for each section? Should look like this:
if([indexPath section] == 0){
return [taskArray count];
} else {
return [completeArray count];
}

how to add 5 different custom cells in tableView

I want to show different data from web in my tableview but I am not getting how to show them in separate cells in one section of a table can any one help me to show
in one cell
cell.textLabel.text=app.i_name;
in second cell
cell.textLabel.text=app.i_phone;
in third cell
cell.textLabel.text=app.i_hours;
in forth cell
cell.textLabel.text=app.i_address;
in fifth cell
cell.textLabel.text=app.i_email;
my cell for row at index is as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Single Cell";
SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"SingleCell" bundle:nil];
cell = (SingleCell *) c.view;
//[c release];
}
appDC * application = [dataArray objectAtIndex:[indexPath row]];
//cell.namelbl.text=application.application_name;
return cell;
}
Try this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Single Cell";
SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"SingleCell" bundle:nil];
cell = (SingleCell *) c.view;
//[c release];
}
appDC * application = [dataArray objectAtIndex:[indexPath row]];
//cell.namelbl.text=application.application_name;
if (indexPath.row == 0)
{
cell.textLabel.text=application.i_name;
}
else if (indexPath.row == 1)
{
cell.textLabel.text = application.i_iphone;
}
else if (indexPath.row == 2)
{
cell.textLabel.text = application.i_hours;
}
else if (indexPath.row == 3)
{
cell.textLabel.text = application.i_address;
}
else
{
cell.textLabel.text = application.i_email;
}
return cell;
}
Hope this answer will help. Cheers
i solved this question by doing this code thanks for all giving suggestion
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellForInfo";
CellForInfo *cell =(CellForInfo *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"CellForInfo" bundle:nil];
cell = (CellForInfo *) c.view;
//[c release];
}
appDC * application = [dataArray objectAtIndex:0];
if (indexPath.row == 0)
{
cell.lblinfo.text=application.i_name;
}
if (indexPath.row == 1)
{
cell.lblinfo.text = application.i_phone;
}
if (indexPath.row == 2)
{
cell.lblinfo.text = application.i_hours;
}
if (indexPath.row == 3)
{
cell.lblinfo.text = application.i_address;
}
if (indexPath.row == 4)
{
cell.lblinfo.text = application.i_email;
}
return cell;
}
IKQ's code example should work. But I suggest to try my TableKit library. This way the code will be more clear and elegant:
- (void)viewDidLoad
{
[super viewDidLoad];
TKStaticCell* nameCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:#"Name" detailText:app.i_name];
TKStaticCell* phoneCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:#"Phone" detailText:app.i_phone];
TKStaticCell* hoursCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:#"Hours" detailText:app.i_hours];
TKStaticCell* addressCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:#"Address" detailText:app.i_address];
TKStaticCell* emailCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:#"email" detailText:app.i_email];
TKSection* section = [TKSection sectionWithCells:nameCell, phoneCell, hoursCell, addressCell, emailCell, nil];
self.sections = [NSArray arrayWithObject:section];
}
Also the library allows to define custom cell instead of TKStaticCell.
You can use this code for short
CellForInfo * cellForInfo = (CellForInfo *)[tableView dequeueReusableCellWithIdentifier:nil];
if (cellForInfo ==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellForInfo" owner:self options:nil];
cellForInfo = [nib objectAtIndex:0];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self tableInfo];
}
- (void) tableInfo
{
NSArray *keysArray = #[#"CampaignsName",#"BusinessName",#"BusinessType",#"CampaignsType",#"Distance",#"Daysleft",#"CampaignImage",];
NSArray *valuesArray1 =[NSArray arrayWithObjects:#"5 % OFF ",#"Coffe Shop",#"1",#"1",#"0.10 Miles",#"5 days Left",#"b2.png", nil];
NSArray *valuesArray2 = [NSArray arrayWithObjects:#"win $2 for you & charity ",#"Red Tommato",#"2",#"2",#"20 Miles",#"2 days Left",#"b1.png", nil];
NSArray *valuesArray3 = [NSArray arrayWithObjects:#"Buy dogs food & get a one more",#"Pet Care",#"3",#"3", #"30 Miles",#"10 days Left",#"b2.png", nil];
NSArray *valuesArray4 =[NSArray arrayWithObjects:#"win $2 for you & charity ",#"Red Tommato",#"1",#"1",#"0.10 Miles",#"7 days Left",#"b2.png", nil];
NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:valuesArray1 forKeys:keysArray];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:valuesArray2 forKeys:keysArray];
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:valuesArray3 forKeys:keysArray];
NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:valuesArray4 forKeys:keysArray];
self.tableArray = [[NSArray alloc]initWithObjects:dict1,dict2,dict3,dict4,dict3,dict1,dict4,dict2,nil];
NSLog(#"Array %#",tableArray);
[self.tableObj reloadData];
}
#pragma mark table view datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableCustomView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
- (UITableViewCell *)tableCustomView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *mainCell ;
static NSString *CellIdentifier1 = #"CustomCellIdentifier";
static NSString *CellIdentifier2 = #"CustomCellIdentifier2";
if (indexPath.row % 2 != 0)
{
CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell2 == nil)
{
NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell2" owner:self options:nil];
cell2 = [topObjects objectAtIndex:0];
}
id object = [self.tableArray objectAtIndex:indexPath.row];
cell2.CampaignsNameLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignsName"]];
cell2.BusinessNameLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"BusinessName"]];
cell2.DistanceLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"Distance"]];
cell2.DaysleftLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"Daysleft"]];
cell2.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignImage"]]];
int businessType = [[NSString stringWithFormat:#"%#",[object valueForKey:#"BusinessType"]] integerValue];
NSLog(#": %d",businessType);
switch (businessType)
{
case 1:
cell2.cellBusinessTypeImg.image = [UIImage imageNamed:#"RETL#2x.png"];
break;
case 2:
cell2.cellBusinessTypeImg.image = [UIImage imageNamed:#"BTY.png"];
break;
case 3:
cell2.cellBusinessTypeImg.image = [UIImage imageNamed:#"t1.png"];
break;
default:
break;
}
int CampaignsType = [[NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignsType"]] integerValue];
switch (CampaignsType)
{
case 1:
cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t3.png"];
break;
case 2:
cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t2.png"];
break;
case 3:
cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t1.png"];
break;
default:
break;
}
mainCell = cell2 ;
}
else
{
CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil)
{
NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell1" owner:self options:nil];
cell1 = [topObjects objectAtIndex:0];
}
id object = [self.tableArray objectAtIndex:indexPath.row];
cell1.CampaignsNameLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignsName"]];
cell1.BusinessNameLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"BusinessName"]];
cell1.DistanceLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"Distance"]];
cell1.DaysleftLbl.text = [NSString stringWithFormat:#"%#",[object valueForKey:#"Daysleft"]];
cell1.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignImage"]]];
int businessType = [[NSString stringWithFormat:#"%#",[object valueForKey:#"BusinessType"]] integerValue];
NSLog(#": %d",businessType);
switch (businessType)
{
case 1:
cell1.cellBusinessTypeImg.image = [UIImage imageNamed:#"RETL#2x.png"];
break;
case 2:
cell1.cellBusinessTypeImg.image = [UIImage imageNamed:#"BTY.png"];
break;
case 3:
cell1.cellBusinessTypeImg.image = [UIImage imageNamed:#"t1.png"];
break;
default:
break;
}
int CampaignsType = [[NSString stringWithFormat:#"%#",[object valueForKey:#"CampaignsType"]] integerValue];
switch (CampaignsType)
{
case 1:
cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t3.png"];
break;
case 2:
cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t2.png"];
break;
case 3:
cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:#"t1.png"];
break;
default:
break;
}
mainCell = cell1 ;
}
return mainCell;
NSLog(#"Index Path is :%d %d", indexPath.section,indexPath.row);
}

Keeping a track of selected cells

In my app, the user changes the fields that appear in a tableView depending on the cells selected by him/her. (FYI... I have permitted multiple cell selection.) Just when the user presses the back button, the program copies the textLabel of the selected cells to the placeholder of the parent viewController.
Here's the relevant section of my code:
- (void)willMoveToParentViewController:(UIViewController *)parent
{
int tempCount = 0;
for (int section = 0; section < 3; section++)
{
int rowMax;
if (section == 0)
rowMax = 3;
else if (section == 1)
rowMax = 5;
else if(section == 2)
rowMax = 3;
for(int row = 0; row < rowMax; row++)
{
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:tempIndexPath];
if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
NSLog(#"tempCount = %d", tempCount);
tempCount++;
if (tempCount == 1)
chosenFieldsViewController.field0.placeholder = selectedCell.textLabel.text;
else if(tempCount == 2)
chosenFieldsViewController.field1.placeholder = selectedCell.textLabel.text;
else if(tempCount == 3)
chosenFieldsViewController.field2.placeholder = selectedCell.textLabel.text;
}
}
}
}
I realized that after selecting the cells if the tableView is scrolled down, the selected cells do not appear as placeHolder on the parentVC. From my analysis I think that once I scroll down, the cells are deleted from the memory. So despite the fact that the cells are selected, they fail to show up on the parent VC.
If so, why do I see the cells appear selected when I scroll up?
I would be grateful if somebody can suggest how I can keep a track of the selected cells even when the user scrolls.
Thanks.
Edit 1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count<3)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
count ++;
}
else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
count--;
}
}
Edit 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"CellForRow%dSection%d",indexPath.row, indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT
if (indexPath.section == 0)
{
switch(indexPath.row)
{
case 0:
cell.textLabel.text = #"Class A";
break;
case 1:
cell.textLabel.text = #"Class B";
break;
case 2:
cell.textLabel.text = #"Class C";
break;
default:
break;
}
}
if(indexPath.section == 1)
{
switch(indexPath.row)
{
case 0:
cell.textLabel.text = #"Class D";
break;
case 1:
cell.textLabel.text = #"Class E";
break;
case 2:
cell.textLabel.text = #"Class F";
break;
case 3:
cell.textLabel.text = #"Class G";
break;
case 4:
cell.textLabel.text = #"Class H";
break;
default:
break;
}
}
if(indexPath.section == 2)
{
switch (indexPath.row)
{
case 0:
cell.textLabel.text = #"Class I";
break;
case 1:
cell.textLabel.text = #"Class J";
break;
case 2:
cell.textLabel.text = #"Class K";
break;
default:
break;
}
}
return cell;
}
Declare a dictionary as,
#property (nonatomic, retain) NSMutableDictionary *selectedTextListDict;
In viewDidLoad,
NSMutableDictionary *selectedTextListDict = [[NSMutableDictionary alloc] init];
Then change these methods as,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count < 3)
{
NSString *rowKeyString = [NSString stringWithFormat:#"%d", indexPath.row];
NSString *sectionKeyString = [NSString stringWithFormat:#"%d", indexPath.section];
NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString];
if (!row) {
row = [[NSMutableDictionary alloc] init];
}
[row setObject:selectedCell.textLabel.text forKey:rowKeyString];
[self.selectedTextListDict setObject:row forKey:sectionKeyString];
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
count ++;
}
else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
NSString *rowKeyString = [NSString stringWithFormat:#"%d", indexPath.row];
NSString *sectionKeyString = [NSString stringWithFormat:#"%d", indexPath.section];
NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString];
[row removeObjectForKey:rowKeyString];
if ([[row allKeys] count] == 0) {
[self.selectedTextListDict removeObjectForKey:sectionKeyString];
} else {
[self.selectedTextListDict setObject:row forKey:sectionKeyString];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
count--;
}
}
- (void)willMoveToParentViewController:(UIViewController *)parent
{
NSArray *selectedTextSectionKeysList = [self.selectedTextListDict allKeys];
NSArray *sortedSelectedTextSectionKeysList = [selectedTextSectionKeysList sortedArrayUsingSelector:#selector(intValue)];
int tempCount = 0;
for (NSString *sectionString in sortedSelectedTextSectionKeysList) {
NSMutableDictionary *rowDict = [self.selectedTextListDict valueForKey:sectionString];
if (rowDict) {
NSArray *selectedTextRowKeysList = [rowDict allKeys];
NSArray *sortedSelectedTextRowKeysList = [selectedTextRowKeysList sortedArrayUsingSelector:#selector(intValue)];
for (NSString *rowString in sortedSelectedTextRowKeysList) {
tempCount++;
if (tempCount == 1)
chosenFieldsViewController.field0.placeholder = [rowDict valueForKey:rowString];
else if(tempCount == 2)
chosenFieldsViewController.field1.placeholder = [rowDict valueForKey:rowString];
else if(tempCount == 3)
chosenFieldsViewController.field2.placeholder = [rowDict valueForKey:rowString];
}
}
}
}
Edit 1:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"GoToModifyFieldsViewController"])
{
ModifyFieldsViewController *modifyFieldsViewController = segue.destinationViewController;
modifyFieldsViewController.chosenFieldsViewController = self;
field0.placeholder = #"";
field1.placeholder = #"";
field2.placeholder = #"";
if(self.selectedTextListDict)
self.selectedTextListDict = [[NSMutableDictionary alloc] init];
}
}
Declare a dictionary in ChosenFieldsViewController: as,
#property (nonatomic, retain) NSMutableDictionary *selectedTextListDict;
In viewDidLoad,
selectedTextListDict = [[NSMutableDictionary alloc] init];
Therefore, rather using self.selectedTextListDict, use: chosenFieldsViewController.selectedTextListDict in ModifyFieldsViewController.
-If so, why do I see the cells appear selected when I scroll up?
Because they get created automatically whenever they are about to appear. Every time you scroll up or down, cells get created or reused. When they disappear, they get destroyed or marked for reuse.

Incorrect updating content of UITableView

I have TableView which number of rows depends on the number of NSStrings in NSMutableArray friendsNames.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return friendsNames.count + 1;
}
Also each row displays that NSString at the respective index of friendsNames. Everything seems to be very simple. But when i remove a string from friendsNames and use reloadData method then weird thing happens: UITableView removes LAST row, not the row with the string which was just removed from friendsNames. Could you please explain me what's going on and what should i do to fix it?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
//create columns
for (int i = 0;i < 6;i++)
[cell.contentView addSubview:[self createGrid:i :indexPath]];
}
return cell;
}
and here is the method which creates columns.it's being called from cellForRowAtIndexPath and it's pretty ugly
- (UILabel *)createGrid:(int)columnIndex :(NSIndexPath *)indexPath
{
CGFloat widths [6] = {35.0,62.0,35.0,35.0,35.0,35.0};//two arrays holding widths of the columns and points where left sides begin
CGFloat leftSides [6] = {0.0,35.0,97.0,132.0,167.0,202.0};
NSArray *titles = [[[NSArray alloc] initWithObjects:#"Status",#"ID",#"Wins",#"Losses",#"Withdrawls",#"Win %", nil] autorelease];
UILabel *columnLabel = [[[UILabel alloc] initWithFrame:CGRectMake(leftSides[columnIndex],0.0,widths[columnIndex], friendsList.rowHeight)] autorelease];
if (indexPath.row == 0)
columnLabel.text = [titles objectAtIndex:columnIndex];
else
{
switch (columnIndex)
{
case 0:
{
BOOL isOnline = [[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:0] boolValue];
columnLabel.text = isOnline ?#"On" :#"Off";
}
break;
case 1:
columnLabel.text = [friendsNames objectAtIndex:indexPath.row - 1];
break;
case 2:
columnLabel.text = [NSString stringWithFormat:#"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:1] intValue] ];
break;
case 3:
columnLabel.text = [NSString stringWithFormat:#"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:2] intValue] ];
break;
case 4:
columnLabel.text = [NSString stringWithFormat:#"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:3] intValue] ];
break;
case 5:
columnLabel.text = [NSString stringWithFormat:#"%f",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:4] floatValue] ];
break;
}
}
columnLabel.layer.borderColor = [[UIColor blackColor] CGColor];
columnLabel.layer.borderWidth = 1.0;
columnLabel.font = [UIFont systemFontOfSize:8.0];
columnLabel.textAlignment = UITextAlignmentCenter;
columnLabel.textColor = [UIColor blackColor];
columnLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
return columnLabel;
}
It's a reusable cell problem. Just change your code like that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *myIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
//Create a new cell
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:myIdentifier] autorelease];
}
//Configure the cell
//Remove all columns
for(UIVIew *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
}
//Create columns
for (int i = 0;i < 6;i++) {
[cell.contentView addSubview:[self createGrid:i :indexPath]];
}
return cell;
}

Set switch to On, reload table then insert a new row

I've got an UITableViewController that I use to create settings of my application.
There is a section with only one row where I put an UISwitch.
How can I insert a new row inside the same section of row with the switch only if the switch in set to YES? And how can I delete this row if the switch is set to NO?
Can anyone help me? Thanks!
I tried to use insertRowsAtIndexPaths:withRowAnimation: method but doesn't work...
This is my settings table code:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Impostazioni", #"");
}
- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}
-(void)addCellToSetCode:(id)sender {
if ([codeSwitch isOn]) {
NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
[[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:#"stateOfSwitch"];
}
else {
NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
[[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:#"stateOfSwitch"];
}
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return NSLocalizedString(#"ListaDesideri", #"");
}
if (section == 1) {
return NSLocalizedString(#"CondivisioneMail", #"");
}
if (section == 2) {
return #"Sicurezza";
}
else {
return nil;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) || (section == 2) || (section == 3) {
return 2;
}
else if (section == 1) {
return 1;
}
else {
return 1;
}
}
- (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] autorelease];
}
if (indexPath.section == 0 && indexPath.row == 0) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
cell.textLabel.text = NSLocalizedString(#"Ordine", #"");
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Nome"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Nome", #"");
}
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Costo"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Costo", #"");
}
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Categoria"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Categoria", #"");
}
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Nome Discendente"]) {
cell.detailTextLabel.text = NSLocalizedString(#"NomeDiscendente", #"");
}
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Costo Discendente"]) {
cell.detailTextLabel.text = NSLocalizedString(#"CostoDiscendente", #"");
}
if ([[defaults objectForKey:#"ordinaPer"] isEqualToString:#"Categoria Discndente"]) {
cell.detailTextLabel.text = NSLocalizedString(#"CategoriaDiscendente", #"");
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 0 && indexPath.row == 1) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
cell.textLabel.text = NSLocalizedString(#"DettagliDesiderio", #"");
if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"costoView"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Costo", #"");
}
if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"descrizioneView"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Descrizione", #"");
}
if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"urlView"]) {
cell.detailTextLabel.text = NSLocalizedString(#"URL", #"");
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 1 && indexPath.row == 0) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
cell.textLabel.text = NSLocalizedString(#"Shortener", #"");
if ([[defaults objectForKey:#"linkShortener"] isEqualToString:#"Nessuno"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Nessuno", #"");
}
if ([[defaults objectForKey:#"linkShortener"] isEqualToString:#"is.gd"]) {
cell.detailTextLabel.text = NSLocalizedString(#"is.gd", #"");
}
if ([[defaults objectForKey:#"linkShortener"] isEqualToString:#"bit.ly"]) {
cell.detailTextLabel.text = NSLocalizedString(#"bit.ly", #"");
}
if ([[defaults objectForKey:#"linkShortener"] isEqualToString:#"TinyURL"]) {
cell.detailTextLabel.text = NSLocalizedString(#"TinyURL", #"");
}
if ([[defaults objectForKey:#"linkShortener"] isEqualToString:#"Linkyy"]) {
cell.detailTextLabel.text = NSLocalizedString(#"Linkyy", #"");
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 2 && indexPath.row == 0) {
cell.textLabel.text = #"Access Code";
codeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 84, 27)];
cell.accessoryView = codeSwitch;
[codeSwitch addTarget:self action:#selector(addCellToSetCode:) forControlEvents:UIControlEventValueChanged];
codeSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"codeSwitchState"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 3 && indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(#"Supporto", #"");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 3 && indexPath.row == 1) {
cell.textLabel.text = NSLocalizedString(#"Informazioni", #"");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
EDIT: Updates below...
I solved part of this problem!
I tried to use [self.tableView reloadData] but doesn't work and casually I solved using [self.tableView setNeedsDisplay]...
Now the switch works but if I set to On it and then I go out from app, completely closing it, the switch doesn't work... How can I solve this?
If this can help other, these are pieces of code updated:
-(void)addCellToSetCode:(id)sender {
if ([codeSwitch isOn]) {
NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
[self.tableView setNeedsDisplay];
[[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:#"codeSwitchState"];
}
else {
NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
[self.tableView setNeedsDisplay];
[[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:#"codeSwitchState"];
}
}
// tableView:numberOfRowsInSection:
else if (section == 2) {
return codeSwitch.on ? 2 : 1;
}
// tableView:cellForRowAtIndexPath:
if (indexPath.section == 2 && indexPath.row == 0) {
cell.textLabel.text = #"Access Code";
codeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 84, 27)];
cell.accessoryView = codeSwitch;
[codeSwitch addTarget:self action:#selector(addCellToSetCode:) forControlEvents:UIControlEventValueChanged];
codeSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"codeSwitchState"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 2 && indexPath.row == 1) {
if ([codeSwitch isOn]) {
cell.textLabel.text = #"Set Access Code";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
At least part of your problem (in the updated code) is that you don't create the UISwitch until you create the cell. Your codeSwitch ivar may end up pointing to a different switch as that table row comes in and out of view.
Here's how I'd do this: in tableView:numberOfRowsInSection:, use the NSUserDefaults to see which state the table should be in, instead of using the state of the switch (which may not exist yet). Then, in the switch's action method, call setBool:forKey: for the user defaults before you insert or delete the table row.
In essence, this makes the code follow the MVC model better, because it separates your view (the UISwitch) from the model (the BOOL in user defaults), with the controller (the view controller) in the middle. By confounding the view and the model (the switch and the boolean state), you end up with problems when trying to deal with the state when the view isn't available yet.
BTW, you shouldn't need to call setNeedsDisplay on the table view at all.