I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}
Related
I want to checkmark the data when the data is being loaded in the uitablecell. I know it works when you put it in the didselect section but can we use it in the cellForRowAtIndexPath. If yes how to use it? I have tried but it doesn't show any checkmarks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"AddGoalsTableViewCell";
AddGoalsTableViewCell *cell = (AddGoalsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AddGoalsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.addGoalsText.font = [UIFont fontWithName:#"AvenirNext-Medium" size:17];
cell.addGoalsText.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
cell.addGoalsText.text = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"name"];
PFObject *currentUser = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"user"];
User *user = [[User alloc] initWithUserId:[currentUser valueForKey:#"userID"] userName:[currentUser valueForKey:#"name"] anduserpassword:[currentUser valueForKey:#"password"]];
if(user==nil)
{
}
else if([[user userId]isEqualToString:[[NSString stringWithFormat:#"%#", [[[UIDevice currentDevice] identifierForVendor] UUIDString] ] lowercaseString]])
{
[self.tableView beginUpdates];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark; <------Here
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
return cell;
}
Please refer following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil )
{
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.lastIndexPath = indexPath;
[tableView reloadData];
}
And lastIndexPath is a property(retain) NSIndexPath* lastIndexPath;
i am facing one module consists to load two different UITableView in same UIViewController i know where i doing mistake, the problem is cell for row AtIndexpath in table view method. i getting only one UITableView in UIViewController but my secondviewcontroller not return any values in cell.
here my sample code for ur reference:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==table) {
return [self.arryData count];
}
else
return [tblArr count];
NSLog(#"tabeCount==>%lu",(unsigned long)tblArr.count);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifier1 = #"Cell";
dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell;
mobilePlanDetailsCellTableViewCell *plan_cell;
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell;
if (tableView==planTable) {
plan_cell = [self.planTable dequeueReusableCellWithIdentifier:CellIdentifier1];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:#"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer4"];
}
return plan_cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setBackgroundColor:[UIColor whiteColor]];
[cell setBackgroundColor:[UIColor whiteColor]];
}
you made the small mistake just change the Return Cell in inside the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
if (tableView==table) {
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
return cell;
}
else {
mobilePlanDetailsCellTableViewCell *plan_cell = [self.planTable dequeueReusableCellWithIdentifier: CellIdentifier];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:#"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:#"answer4"];
return plan_cell;
}
}
The problem is that you are returning the cell outside the if (tableView==table), so your program will NOT reach the code after it.
Instead of
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell; //This line is wrong
You should do this:
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
//Should be inside the if scope
return cell;
}
Why you are doing such complex coding, Just simply provide different tag value for Table Views, and you are returning cell outside of your if condition which is wrong. Follow the below code.
Do a checking like this:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag==1)// Table 1
{
//load cell as per your choice
//cell operations
return cell;
}
else // Table 2
{
//load cell as per your choice
//cell operations
return cell;
}
Why the UITableViewCells don't reload the checkmarks after selecting, scrolling away, then scrolling back?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?#"":str
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:#"#669900"];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
[[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:#"#669900"]];
if (cell == nil) {
cell = [[UITableViewCell alloc] init];
}
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [[stateArray objectAtIndex:indexPath.row] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(#"It hit showCheckmark = YES, and stateArray is %#",stateArray[indexPath.row]);
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"It hit showCheckmark = NO, and stateArray is %#",stateArray[indexPath.row]);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[stateArray insertObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row];
[selectedObjects addObject:object];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[stateArray insertObject:[NSNumber numberWithBool:NO] atIndex:indexPath.row];
[selectedObjects removeObject:object];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You missed out the ! (inverse operator) on the following line meaning that the state will always be the same.
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:[[stateArray objectAtIndex:indexPath.row] boolValue]]];
It should be
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:![[stateArray objectAtIndex:indexPath.row] boolValue]]];
Edit --- I've refactored both methods because it can be done with a lot less code and it will completely simplify the methods for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [stateArray[indexPath.row] boolValue];
if (showCheckmark == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedObjects addObject:object];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedObjects removeObject:object];
}
stateArray[indexPath.row] = #(cell.accessoryType == UITableViewCellAccessoryCheckmark);
}
I suggest a more object oriented approach. This will ensure that your code is flexible and displays correctly all the time.
For each item you wish to display in your table, have a corresponding object. You mentioned that you are displaying contacts, so let's suppose your object is called "Contact":
//Contact.h
#interface Contact : NSObject
#property BOOL selected;
#property NSString *name;
#end
//Contact.m
#import Contact.h
#implementation Contact
+ (id) contactWithName:(NSString*)name {
Contact *nContact = [Contact new];
nContact.name = name;
nContact.selected = NO;
return nContact;
}
#end
Then, just make your view work something like this:
//ContactView.m
#interface ContactView()
#property NSMutableArray *contacts;
#end
#implementation ContactView
#synthesize contacts;
- (void) viewDidLoad {
[super viewDidLoad];
//get your contact list here. When creating contacts, be sure to assign their selected and their name as you require.
contacts = #[[Contact contactWithName:#"John"], [Contact contactWithName:#"Jane"]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"inviteCell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cell.textLabel.text = cellContact.name;
cell.accessoryType = cellContact.selected == YES ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cellContact.selected = !cellContact.selected;
[contacts replaceObjectAtIndex:indexPath.row withObject:cellContact];
[tableView reloadData]; //to refresh without animation
//[tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])] withRowAnimation:UITableViewRowAnimationTop]; //to refresh with animation
}
#end
And boom, easy to use tables that always look right, queue properly, and are object oriented for easy maintenance later.
The cell selection problem was not solved, even when insertObject was replaced with replaceWithObject, however, one should not waste time setting BOOL objects with an NSInteger inside an NSMutableArray. Instead, for cell selection memory, one should use NSDictionary like this:
#property (nonatomic, strong) NSMutableDictionary * selectedRowCollection;
- (void)viewDidLoad{
self.selectedRowCollection = [[NSMutableDictionary alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowCollection setObject:#"1" forKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowCollection removeObjectForKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL showCheckmark = [[self.selectedRowCollection valueForKey:[NSString stringWithFormat:#"%d",indexPath.row]] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You are not doing this UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Allocating is necessary if cell is nil. Or it cause problem while scrolling.
I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
return cell;
}
So far search on Stack Overflow I havent found a situation that is like mine. Any assistance is greatly appreciated: I keep seeing that if I put a checkmark on Person A, Person H will also have one as well as does a person about 10 away. Basically abut every 10 it repeats a check mark.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text =
[NSString stringWithFormat:#"%# %#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"FirstName"],[[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"LastName"]];
cell.detailTextLabel.text =
[NSString stringWithFormat:#"%#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"Address"]];
return cell;
}
In my did select row for index path I have this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [self.tableView cellForRowAtIndexPath: indexPath];
if ([[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"emailSelected"] != #"YES")
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"YES" forKey:#"emailSelected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"NO" forKey:#"emailSelected"];
}
This is due to how UITableView "recycles" UITableViewCell for efficiency purposes, and how you are marking your cells when they are selected.
You need to refresh/set the accessoryType value for every cell you process/create within tableView:cellForRowAtIndexPath:. You properly update the state in your myArrayOfAddressBooks data structure, and you just need to use this information in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *info = [myArrayOfAddressBooks objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", [info objectForKey:#"FirstName"],[info objectForKey:#"LastName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [info objectForKey:#"Address"]];
cell.accessoryType = ([[info objectForKey:#"emailSelected"] isEqualString:#"YES"]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
Also, unless there is a good reason for saving the state as #"Yes" or #"No" strings, why not save them as [NSNumber numberWithBool:YES] or [NSNumber numberWithBool:NO]? This will simplify your logic when you want to do comparisons versus having to use isEqualToString: all the time.
e.g.
cell.accessoryType = ([[info objectForKey:#"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;