4

我有一个 tableView,我在其中选择单元格并将数据添加到数组中,我还可以选择滑动然后删除最终从数组中删除数据的特定单元格。

问题是,一旦我删除了一行,我在重新加载表格后就失去了所有的选择状态,

为此,我再次检查了选择数组并重新选择了所有这些单元格,

但是我被困在一个地方,在我真正删除一个单元格并重新加载 tableView 之前,只要我在一个单元格上滑动,所有其他单元格的选择状态也会消失。

注意:我有两个数组,一个包含要在 tableView 中显示的项目列表,一个包含所选项目。

这是一些代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;  
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.contactList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell.textLabel setTextColor:[UIColor colorWithRed:103.0/255.0 green:103.0/255.0 blue:103.0/255.0 alpha:1.0]];
    [cell.textLabel setFont:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:14.0]];

    if (![[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"nickName"] isEqualToString:@""])
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"nickName"]];

    else
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"firstName"],[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"lastName"]];

    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

    NSLog(@"Selected cell index==>%d\n",indexPath.row);
    //NSString *emailID = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"email_key"]];
    NSLog(@"emailID==>%@\n",[self.contactList objectAtIndex:indexPath.row]);    
    [self.emailShareList addObject:[self.contactList objectAtIndex:indexPath.row]];
    //[self.emailShareList insertObject:emailID atIndex:indexPath.row];
    NSLog(@"Array value==>%@\n",self.emailShareList);
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"deSelected cell index==>%d\n",indexPath.row);
    NSString *emailID = [NSString stringWithFormat:@"%@",[[self.contactList objectAtIndex:indexPath.row] objectForKey:@"email_key"]];
    NSLog(@"emailID==>%@\n",emailID);   

    [self.emailShareList removeObject:[self.contactList objectAtIndex:indexPath.row]];
    NSLog(@"deSelect row Array value==>%@\n",self.emailShareList);
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        if(indexPath.row != 0)
        {
            NSString *contactID = [[self.contactList objectAtIndex:indexPath.row] objectForKey:@"contactId"];
            NSLog(@"content on delete row==>%@\n",contactID);
            [self.contactList removeObjectAtIndex:indexPath.row];
            [self deleteContactToServer:contactID];
        }
    }

    [contactTableView reloadData];
    for (int i = 0; i < [self.emailShareList count]; i++) 
    {
        for (int j = 0; j < [self.contactList count]; j++) 
        {
            if([[[self.contactList objectAtIndex:j] valueForKey:@"email"]   isEqualToString:         [[self.emailShareList objectAtIndex:i] valueForKey:@"email"]])
            {
                NSIndexPath *path1 = [NSIndexPath indexPathForRow:j inSection:0];
                [contactTableView selectRowAtIndexPath:path1 animated:NO scrollPosition:UITableViewScrollPositionNone];
            }
        }    
    }
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{




    UITableViewCellEditingStyle style = UITableViewCellEditingStyleNone;

    if(indexPath.row != 0)
        style = UITableViewCellEditingStyleDelete;


    return style;
}
4

2 回答 2

5

当您删除一个项目时,您不必重新加载整个表格视图。您可以使用 – deleteRowsAtIndexPaths:withRowAnimation: 方法来删​​除有问题的单元格(以及相应的模型更新)。这可能会保留您的选择。

要在进入编辑模式时保留您的选择(滑动删除也是编辑模式的一种特殊情况),您需要做两件事:

首先,在您的 tableView 上启用allowSelectionDuringEditing:

self.tableView.allowsSelectionDuringEditing = YES;

其次,创建一个 UITableView 子类并覆盖 setEditing:animated: 像这样:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    NSArray *indexPaths = self.indexPathsForSelectedRows;
    [super setEditing:editing animated:animated];
    for (NSIndexPath *ip in indexPaths) {
        [self selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

就个人而言,当从模型的角度来看,我宁愿使用某种自定义选择机制。我会创建一个自定义单元格子类,向它添加一个选择属性,让它相应地更改单元格样式。影响常规表格视图选择的内置功能不会导致这种方法出现问题。

于 2012-08-22T12:04:50.160 回答
0

这是在编辑模式下保留表格选择的另一种方法,而无需继承 UITableView。将以下内容添加到您的 UITableViewControllerView。

在 viewDidLoad 添加:

self.tableView.allowsSelectionDuringEditing = YES;

然后覆盖 setEditing:animated:

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
    NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];

    [super setEditing:editing animated:animate];

    for (NSIndexPath *selectedIndexPath in selectedIndexPaths) {
        [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}
于 2014-07-01T00:27:19.893 回答