当UITableView向上移动时,UITableViewCellAccessoryCheckmark会移除吗?

时间:2013-07-16 06:18:52

标签: uitableview

我会检查didSelectRow方法,但是当我向上移动时,它会删除检查 我认为它创造了新的细胞

如何使用唯一单元格,以便不会取消选中的已选中单元格。

- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"MY INDEX PATH IS %@", indexPath);

    NSString *email = [allEmails objectAtIndex:indexPath.row];

    if (tableAlert.type == SBTableAlertTypeMultipleSelct) {

        UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];

        if (cell.accessoryType == UITableViewCellAccessoryNone){
             [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
             [selectedEmail addObject:email];
        }

        else{
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            [selectedEmail removeObject:email];

        }

       [tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES];
 }

     NSLog(@"Final Array is %@", selectedEmail);
 }


- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;

    // NSString *identifier = [NSString stringWithFormat:@"%d%d", indexPath.section, indexPath.row];


if (tableAlert.view.tag == 0 || tableAlert.view.tag == 1) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
} else {
    // Note: SBTableAlertCell
    cell = [[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

//[cell.textLabel setText:[NSString stringWithFormat:@"Cell %d", indexPath.section]];

NSString *email = [allEmails objectAtIndex:indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%@", email];


UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 14.0 ];
cell.textLabel.font  = myFont;

return cell;
}

1 个答案:

答案 0 :(得分:0)

由于您已将选定的电子邮件存储在 selectedEmail 数组中,因此您可以在 cellForRowAtIndexPath 方法中使用它来显示复选标记:

NSString *email = [allEmails objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", email];
if ([selectedEmail indexOfObject:email] != NSNotFound) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

通过这种方式,即使重复使用该单元,也不会有任何问题。