How to store, retrieve check or unchecked cells - ios

I am trying to save the checkmark that i place on a cell and then retrieve it. I have 11 cells in that section.
I tried to use this link as a guide and it works. But i want only need one checkmark to be saved rather than many.
And also i want to set checkmark for all cells by default. How would i achieve this
My Code :
- (NSString *)getKeyForIndex:(int)index{
return [NSString stringWithFormat:#"KEY%d",index];
}
- (BOOL) getCheckedForIndex:(int)index {
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES) {
return YES;
}
else
{
return NO;
}
}
- (void) checkedCellAtIndex:(int)index
{
BOOL boolChecked = [self getCheckedForIndex:index];
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (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(section == 4) {
if([self getCheckedForIndex:indexPath.row]==YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
NSString *key = [_keys objectAtIndex:indexPath.section];
NSArray *name = [_names objectForKey:key];
static NSString *MyCellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [name objectAtIndex:indexPath.row];
if(section == 4) {
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Solution can be tricky :
Maintain one array same to tableview datasource array.
Say for example u have NSMutableArray *mutArrNames which tableview datasource. Now NSMutableArray *mutArrNamesCheckListwhich shows which one is checked or not;
Intially nothing is selected so add 0 to uncheck one:
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"0"];
}
Now use like this:
- (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(section == 4) {
//check from mutArrNamesCheckList's list if cell is check one or not
if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else //unchecked one
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
Here it can be used according to your requirement like allow mutiple selection or single selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get cell object
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(section == 4)
{
if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//replace 0 with 1 as check one
[mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:#"0"];
}
else //unchecked one
{
cell.accessoryType = UITableViewCellAccessoryNone;
//replace 1 with 0 as uncheck one
[mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:#"1"];
}
}
}
For selection and unselection of all cell add this in button's action method. Use this :
//remove all as new objects will be added
[mutArrNamesCheckList removeAllObjects];
if(!btnCheckAll.selected)//selected property used - default is no so all not selected
{
//make all selected
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"1"];
}
//change buttons image here
}
else
{
//make all unselected
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"0"];
}
//change buttons image here
}
//tableView reload
[yourtableView reloadData];
btnCheckAll.selected = !btnCheckAll.selected;
Add mutArrNamesCheckList in AppDelegate to be visible in whole Application.
So remove iteration we done on top of above answer in our viewcontroller as we didn't wanted to use gobally
EDIT : added toggling in didSelectRowAtIndexPath + clicked select all button, all cell is checked else unchecked

Related

Checkmark in tableViewCell hidden when Scroll

When i scroll Table, checkmark is hide. I know because of Reusing Cell, but I dont know how to fix. Pls help me. Here is my code:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
I change check and uncheck in didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[dict setObject:courseStudent.id_user forKey:#"student_id"];
[studentDetail addObject:dict];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}
When you scroll table view, cellForRowAtIndexPath for particular cell will be called in which your are setting the accessoryType as None. Instead try like below.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
if ([studentDetail containsObject:courseStudent]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[dict setObject:courseStudent.id_user forKey:#"student_id"];
[studentDetail addObject:dict];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}
Hope this will help.
If you are reusing cells then you need to save the state of each cell .Because every time you scroll up and down, TableView will bring back the previous cell that outside of the screen.
You can save selectable state in an array and read its index in cellForRowAtIndexpath to get the current state of the cell.
You can also do this by adding one boolean property like is-selected in your "TeacherInfo" NSObject class,and set true false based on table-row selection.
Try this :
You need to add instance object of TeacherInfo instead of student id because your containedObject of array gives wrong result .
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
if ([studentDetail containsObject:courseStudent]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[studentDetail addObject:courseStudent];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}

how to show selected rows with check marks while updating the previously selected rows in UITableView in objective c

i tried like this...in cellForRowAtIndex()
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableViewIdentifier=#"SimpleTableViewIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row];
return cell;
}
and added below lines of code in didSelectRowAtIndexPath..
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
but,while updating the previous selected value,check mark is not showing onpreviously selected rows..any one can help in this issue..Thanks in advance.. :)
I think this is what you need to do. In your cellForRowAtIndexPath method, instead of assigning cell.selectionStyle = UITableViewCellSelectionStyleNone; for all cells, assign it to none only for those cells which are not selected
Change the didSelectRowAtIndexPath method to store the selected cell detail to an array
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedCellArray addObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Now change the cellForRowAtIndexPath method like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableViewIdentifier=#"SimpleTableViewIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier];
}
cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row];
if (![self.selectedCellArray containsObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
I got it Tamim. Check the below answer.I tried sample project.It works fine.
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark;
NSArray *arrayFetchFromDefaults;
NSInteger lastSelectedIndex;
}
#end
#implementation ViewController
#synthesize tableViewCheckMarkSelectionUpdate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrProductSelection = [[NSMutableArray alloc]initWithObjects:#"iPhone",#"iPad",#"iPod",#"iTV",#"iWatch",#"iMac",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
arrayFetchFromDefaults = [userDefaults objectForKey:#"selectedcheckmark"];
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults];
if(arrProductSelectDeSelectCheckMark.count == 0)
{
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
for(int j=0;j<[arrProductSelection count];j++)
{
[arrProductSelectDeSelectCheckMark addObject:#"deselected"];
}
}
[tableViewCheckMarkSelectionUpdate reloadData];
}
#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrProductSelection.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strCell = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
// lastSelectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:#"selectedRow"]; - Getting Last selected index row
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:#"deselected"])
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// if (indexPath.row == lastSelectedIndex)
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:#"selectedRow"]; //This is for Last Selected IndexPath Row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
#try
{
CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate];
NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint];
NSLog(#"%#",arrProductSelectDeSelectCheckMark);
if([arrProductSelectDeSelectCheckMark count]==0)
{
for(int i=0; i<[arrProductSelection count]; i++)
{
[arrProductSelectDeSelectCheckMark addObject:#"deselected"];
}
}
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:#"deselected"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:#"selected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:#"deselected"];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:arrProductSelectDeSelectCheckMark forKey:#"selectedcheckmark"];
[defaults synchronize];
}
#catch (NSException *exception) {
NSLog(#"The exception is-%#",exception);
}
}
- (IBAction)actionGoPrevious:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
#end

checkmark does not appear on first cell

i need to add checkmark when we tap on each cell, and it must show when i return back to tableview.
I tried with this code but its not working,
- (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 (self.thread == nil) {
cell.textLabel.text = #"Loading, please wait...";
} else if ([self.thread count] == 0) {
cell.textLabel.text = #"No Results!";
} else {
//
NSDictionary *msg = [self.thread objectAtIndex:indexPath.row];
NSLog(#"yourMutableDictionary - %#",msg);
if (indexPath.row == 0) {
cell.textLabel.text = [thread objectAtIndex:0];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"%# ", [msg objectForKey:#"id"]];
if ([self.idSelected count] != 0) {
if ([self.idSelected containsObject:[NSString stringWithFormat:#"%#", [msg objectForKey:#"id"]]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
}
return cell;
}
#pragma mark - Table view delegate
//In a xib-based application, navigation from a table can be handled in - tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
{
if (self.thread == nil || self.thread.count == 0) {
return;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (path.row == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;// this checkmark not working
}
else {
NSDictionary *user = [self.thread objectAtIndex:path.row];
NSString *uName = [NSString stringWithFormat:#"%# %#", [user objectForKey:#"first_name"], [user objectForKey:#"last_name"]];
NSString *uId = [NSString stringWithFormat:#"%#",[user objectForKey:#"id"]];
NSLog(#" click id%#", uId);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[userNames removeObject:uName];
[userIds removeObject:uId ];
cell.accessoryType = UITableViewCellAccessoryNone; // this check mark is working perfectly
} else {
[userNames addObject:uName];
[userIds addObject:uId];
cell.accessoryType = UITableViewCellAccessoryCheckmark; // this check mark is working perfectly
}
}
}
first cell check mark is not working but others working perfectly. I want to add check mark on first cell too, how its possible
Use below code to display checkmark on multiple table view cell :
- (void)viewDidLoad
{
[super viewDidLoad];
self.arraySelectedCell = [NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.arraySelectedCell containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// do here..
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//the below code will allow multiple selection
if ([self.arraySelectedCell containsObject:indexPath])
{
[self.arraySelectedCell removeObject:indexPath];
}
else
{
[self.arraySelectedCell addObject:indexPath];
}
[tableView reloadData];
}
happy coding!

ios TableViewcell set checkmark but scrolling time checkmark is Auto hide and try to solve it creash

i have a tableview cell and this is binding from one array and select to multipal cell and set to checkmark but when i scroll table the checkmark is auto hide
and then i search on stackoverflow and i find one answier
the link is
UITableview accessory type disappear while scrolling
i tried this code but app is crash Here is my code, please Help me
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"FirstTableViewCell";
FirstTableViewCell *cell = (FirstTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FirstTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(tableView ==nurseTypeTable){
cell.lbl1.text = [arrayNurseType objectAtIndex:indexPath.row];
if(_nurseArray.count != nil){
// if (_nurseArray == nil || [_nurseArray count] == 0) {
NSNumber *rowNsNum = [_nurseArray objectAtIndex:indexPath.row];
if ( [arrayNurseType containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * cellValue=nil;
if (tableView==nurseTypeTable) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Here is for Check
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//here i add in array
cellValue=[arrayNurseType objectAtIndex:indexPath.row];
NSLog(#"%#",cellValue);
[_nurseArray addObject:cellValue];
NSLog(#"%#",_nurseArray);
}
//Here is for uncheck
else {
cellValue=[arrayNurseType objectAtIndex:indexPath.row];
[_nurseArray removeObject:cellValue];
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Remove Array %#",_nurseArray);
}
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
in function cellForRowAtIndexPath **
NSNumber *rowNsNum = [_nurseArray objectAtIndex:indexPath.row];** is creash on debug time
arrayNurseType = [NSMutableArray arrayWithObjects:#" Nurse ",#"One",nil]
You should use
if(tableView ==nurseTypeTable){
cell.lbl1.text = [arrayNurseType objectAtIndex:indexPath.row];
if(_nurseArray.count != nil){
// if (_nurseArray == nil || [_nurseArray count] == 0) {
if ( [_nurseArray containsObject:indexPath] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
}

Select one speicific row with one checkmark using UITbaleView

Plz anyone tell me how i select the row at one time with checkmark while other row are not checkmark.I tried but in my case there is mutiple row selected with checkmark. Basically I want to save one row with checkmark when i select another row then previous row deselect and this row select with checkmark. Here is my Code
- (NSString *)getKeyForIndex:(int)index
{
return [NSString stringWithFormat:#"KEY%d",index];
}
- (BOOL) getCheckedForIndex:(int)index
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]]boolValue]==YES)
{
return YES;
}
else
{
return NO;
} }
- (void) checkedCellAtIndex:(int)index
{ BOOL boolChecked = [self getCheckedForIndex:index];
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return List.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *subviewCells = #"Cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells];
}
cell.textLabel.text = [List objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
selectLanguage = [List objectAtIndex:indexPath.row];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==NO)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
selectLanguage = [List objectAtIndex:indexPath.row];
}
#end;
Try this :
Single Row Selection:
create a new variable to track the index In Controller:
int selectedIndex;
in UITableView cellForRowAtIndexPath method:
if(indexPath.row == selectedIndex)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
and in UITableView didSelectRowAtIndex method:
selectedIndex = indexPath.row;
[tableView reloadData];
2 way as per you want:
.h file:
NSIndexPath* checkedIndexPath;
#property (nonatomic, retain) NSIndexPath* checkedIndexPath;
.m file:
#synthesize checkedIndexPath;
- (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];
}
//do you stuff here
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//do work for checkmark
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
}
Happy coding!!

Resources