带复选标记的 UITableViewCell,复制复选标记 [英] UITableViewCell with Checkmark, duplicating checkmarks
问题描述
到目前为止在 Stack Overflow 上搜索我还没有找到像我这样的情况.非常感谢任何帮助:我一直看到,如果我在 A 上打勾,H 也会有一个,以及大约 10 外的一个人.基本上每 10 次它就会重复一个复选标记.
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.
这是我的代码:
- (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"];
}
推荐答案
这是由于 UITableView
如何回收" UITableViewCell
以提高效率,以及您如何标记选中的单元格.
This is due to how UITableView
"recycles" UITableViewCell
for efficiency purposes, and how you are marking your cells when they are selected.
您需要为您在 tableView:cellForRowAtIndexPath:
中处理/创建的每个单元格刷新/设置 accessoryType
值.您正确更新 myArrayOfAddressBooks
数据结构中的状态,您只需要在 tableView:cellForRowAtIndexPath:
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;
}
此外,除非有充分的理由将状态保存为 @"Yes"
或 @"No"
字符串,为什么不将它们保存为 [NSNumber numberWithBool:YES]
还是 [NSNumber numberWithBool:NO]
?当您想要进行比较而不是一直使用 isEqualToString:
时,这将简化您的逻辑.
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.
例如
cell.accessoryType = ([[info objectForKey:@"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
这篇关于带复选标记的 UITableViewCell,复制复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!