I have build a grouped tableview with 2 sections.
There always have to be 1 selection in each of the sections, and there can't be more.
This is working rock steady when testing on the iPad.
I recently started developing the xib for the iPhone version.
Now things start getting weird. When scrolling the selections fly all over the place, disappears, and sometimes there are 4 selections in a section.
The exact same code is working flawlessly on the iPad.
Is there any known bugs that might make this occur?
UPDATED WITH CODE:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:#"Baskerville", #"Palatino", #"Times New Roman", #"Verdana", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
NSArray *countriesLivedInArray = [NSArray arrayWithObjects:#"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", #"My Text", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:#"Countries"];
[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];
//Set the title
self.navigationItem.title = #"Countries";
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
//SET Font
NSString *neverOpendFonts1 = [prefs objectForKey:#"neverOpendFonts1"];
if (![neverOpendFonts1 isEqualToString:#"1"]) {
lastIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *lastIndexPathString1 = [NSString stringWithFormat:#"1"];
[prefs setObject:lastIndexPathString1 forKey:#"lastIndexPath1"];
NSString *fontName = [NSString stringWithFormat:#"Palatino"];
[prefs setObject:fontName forKey:#"fontName"];
neverOpendFonts1 = [NSString stringWithFormat:#"1"];
[prefs setObject:neverOpendFonts1 forKey:#"neverOpendFonts1"];
[prefs synchronize];
}
else
{
NSInteger row = [[prefs objectForKey:#"lastIndexPath1"] intValue];
lastIndexPath1 = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
//SET Font SIZE
NSString *neverOpendFonts2 = [prefs objectForKey:#"neverOpendFonts2"];
if (![neverOpendFonts2 isEqualToString:#"1"]) {
lastIndexPath2 =[NSIndexPath indexPathForRow:2 inSection:1];
UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath2];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *lastIndexPathString2 = [NSString stringWithFormat:#"2"];
[prefs setObject:lastIndexPathString2 forKey:#"lastIndexPath2"];
NSString *fontSize = [NSString stringWithFormat:#"24.0"];
[prefs setObject:fontSize forKey:#"fontSize"];
neverOpendFonts2 = [NSString stringWithFormat:#"1"];
[prefs setObject:neverOpendFonts2 forKey:#"neverOpendFonts2"];
[prefs synchronize];
}
else
{
NSInteger row2 = [[prefs objectForKey:#"lastIndexPath2"] intValue];
lastIndexPath2 = [NSIndexPath indexPathForRow:row2 inSection:1];
UITableViewCell *newCell2 = [myTableView cellForRowAtIndexPath:lastIndexPath2];
newCell2.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"Countries"];
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [listOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
//First get the dictionary object
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Countries"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if (indexPath.section == 0)
{
switch (indexPath.row) {
case 0:
{
cell.textLabel.font = [UIFont fontWithName:#"Baskerville" size:24];
}
break;
case 1:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:24];
}
break;
case 2:
{
cell.textLabel.font = [UIFont fontWithName:#"Times New Roman" size:24];
}
break;
case 3:
{
cell.textLabel.font = [UIFont fontWithName:#"Verdana" size:24];
}
break;
}
}
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:16];
}
break;
case 1:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:20];
}
break;
case 2:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:24];
}
break;
case 3:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:28];
}
break;
case 4:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:32];
}
break;
case 5:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:36];
}
break;
case 6:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:40];
}
break;
case 7:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:44];
}
break;
case 8:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:48];
}
break;
case 9:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:52];
}
break;
case 10:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:56];
}
break;
case 11:
{
cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:60];
}
break;
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// CGFloat result;
if (indexPath.section == 0)
{
return 50;
}
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
{
return 50;
}
break;
case 1:
{
return 50;
}
break;
case 2:
{
return 50;
}
break;
case 3:
{
return 50;
}
break;
case 4:
{
return 50;
}
break;
case 5:
{
return 50;
}
break;
case 6:
{
return 50;
}
break;
case 7:
{
return 54;
}
break;
case 8:
{
return 58;
}
break;
case 9:
{
return 62;
}
break;
case 10:
{
return 66;
}
break;
case 11:
{
return 70;
}
break;
}
}
return 50;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Font";
}
if (section == 1) {
return #"Font Size";
}
else if (!section == 0 || !section == 1) {
return #"Text";
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger newRow = [indexPath row];
if (indexPath.section == 0) {
NSString *fontName;
switch (indexPath.row) {
case 0:
{
fontName = [NSString stringWithFormat:#"Baskerville"];
}
break;
case 1:
{
fontName = [NSString stringWithFormat:#"Palatino"];
}
break;
case 2:
{
fontName = [NSString stringWithFormat:#"Times New Roman"];
}
break;
case 3:
{
fontName = [NSString stringWithFormat:#"Verdana"];
}
break;
}
NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath1 = indexPath;
}
NSInteger lastRow = lastIndexPath1.row;
NSString *lastIndexPathString = [NSString stringWithFormat:#"%i", lastRow];
[prefs setObject:lastIndexPathString forKey:#"lastIndexPath1"];
[prefs setObject:fontName forKey:#"fontName"];
[prefs synchronize];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if (indexPath.section == 1) {
NSString *fontSize;
switch (indexPath.row) {
case 0:
{
// cell.textLabel.font = [UIFont fontWithName:#"Palatino" size:16];
fontSize = [NSString stringWithFormat:#"16.0"];
// UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
// newCell = [tableView cellForRowAtIndexPath:indexPath];
// newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
break;
case 1:
{
fontSize = [NSString stringWithFormat:#"20.0"];
}
break;
case 2:
{
fontSize = [NSString stringWithFormat:#"24.0"];
}
break;
case 3:
{
fontSize = [NSString stringWithFormat:#"28.0"];
}
break;
case 4:
{
fontSize = [NSString stringWithFormat:#"32.0"];
}
break;
case 5:
{
fontSize = [NSString stringWithFormat:#"36.0"];
}
break;
case 6:
{
fontSize = [NSString stringWithFormat:#"40.0"];
}
break;
case 7:
{
fontSize = [NSString stringWithFormat:#"44.0"];
}
break;
case 8:
{
fontSize = [NSString stringWithFormat:#"48.0"];
}
break;
case 9:
{
fontSize = [NSString stringWithFormat:#"52.0"];
}
break;
case 10:
{
fontSize = [NSString stringWithFormat:#"56.0"];
}
break;
case 11:
{
fontSize = [NSString stringWithFormat:#"60.0"];
}
break;
}
NSInteger oldRow = (lastIndexPath2 != nil) ? [lastIndexPath2 row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath2];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath2 = indexPath;
}
NSInteger lastRow2 = lastIndexPath2.row;
NSString *lastIndexPathString2 = [NSString stringWithFormat:#"%i", lastRow2];
[prefs setObject:lastIndexPathString2 forKey:#"lastIndexPath2"];
//NSString *lastIndexPathString = [NSString stringWithFormat:#"#%", lastIndexPath2];
//[prefs setObject:lastIndexPathString forKey:#"lastIndexPath2"];
[prefs setObject:fontSize forKey:#"fontSize"];
[prefs synchronize];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
TSolved the problem... Got my answer on iphonedevsdk.com...
I added
if([self.lastIndexPath1 isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
to the cellForRowAtIndexPath
and added
if(self.lastIndexPath1)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.lastIndexPath1];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.lastIndexPath1 = indexPath;
}
to didSelectRowAtIndexPath...
I removed the old `/*
NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath1 = indexPath;
}
*/`
from didSelectRow, and now everything works perfectly...
Related
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;
}
}
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);
}
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.
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;
}
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.