log*_*ist 0 xcode uitableview ios
到目前为止搜索Stack Overflow我还没有找到像我一样的情况.非常感谢任何帮助:我一直看到,如果我在A人身上加上一个勾号,那么H人也会有一个人以及一个人约10个人.基本上每10个它重复一次复选标记.
这是我的代码:
- (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;
Run Code Online (Sandbox Code Playgroud)
}
在我的索引路径的选择行中我有这个:
- (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"];
}
Run Code Online (Sandbox Code Playgroud)
这是因为为了提高效率而UITableView"回收" UITableViewCell的方式,以及在选择细胞时如何标记细胞.
您需要刷新/设置accessoryType您在其中处理/创建的每个单元格的值tableView:cellForRowAtIndexPath:.您正确更新了myArrayOfAddressBooks数据结构中的状态,您只需要在其中使用此信息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;
}
Run Code Online (Sandbox Code Playgroud)
另外,除非有充分的理由将状态保存为字符串,@"Yes"否则@"No"为什么不将它们保存为[NSNumber numberWithBool:YES]或[NSNumber numberWithBool:NO]?当您想要进行比较而不是一直使用时,这将简化您的逻辑isEqualToString:.
例如
cell.accessoryType = ([[info objectForKey:@"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
Run Code Online (Sandbox Code Playgroud)