0

我确实检查了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;
}
4

1 回答 1

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];
}

这样,即使重复使用单元格,也不会有任何问题。

于 2013-07-16T07:46:39.597 回答