Reputation: 3834
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"];
}
Upvotes: 0
Views: 994
Reputation: 3208
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;
Upvotes: 6