2012-08-22 104 views
3

我有一個tableView,我選擇單元格並將數據添加到數組中,我也可以選擇滑動,然後刪除特定的單元格,最終從數組中刪除數據。如何保持原始細胞的完整選擇?

的問題是,一旦我刪除行,我失去了我所有的選擇狀態我重裝表,

對於我的選擇陣列檢查了一遍,重新選擇所有這些細胞後,

BUT我被困在一個地方,在我實際刪除一個單元格並重新加載tableView之前,只要我滑過單元格,所有其他單元格的選擇狀態也會消失。

注意:我有兩個數組,其中一個具有要在tableView中顯示的itmes列表,另一個具有所選項目。

下面是一些代碼:

- (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; 
} 
+0

你會在這裏發佈示例代碼? –

回答

3

當你刪除一個項目,你沒有必要重新加載整個的tableview。您可以使用 - deleteRowsAtIndexPaths:withRowAnimation:方法來刪除有問題的單元格(以及相應的模型更新)。這可能會保留你的選擇。

爲了讓您的選擇時進入編輯模式(刷卡的刪除是編輯模式的特殊情況也一樣),你的東東做兩件事情:

首先,在您的tableView啓用allowsSelectionDuringEditing:

self.tableView.allowsSelectionDuringEditing = YES; 

二,創建一個UITableView子類並覆蓋setEditing:動畫:是這樣的:

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

就個人而言,我寧願使用某種形式的自定義選擇機制,當從模型角度選擇重要時。我會創建一個自定義單元格子類,向它添加一個選擇屬性,讓它相應地更改單元格樣式。影響常規表格視圖選擇的內置功能不會導致這種方法出現問題。

+0

謝謝,它的工作原理。但是,滑動編輯和允許MultipleSelectionDuringEditing是衝突的。 我希望能夠選擇多個單元格,同時也可以輕掃並刪除單個單元格。 –

+0

我注意到了。爲了達到這個目的,你可能會拿出你自己的選擇方案,正如我在答覆的第二部分所概述的那樣。 –

+0

這似乎是唯一的選擇。我只是在檢查,如果我可以脫身而不定製細胞。 [懶惰] –

0

這是另一種保存表格選擇進出編輯模式的方法,無需子類化UITableView。將以下內容添加到您的UITableViewControllerView。

在viewDidLoad中添加:

self.tableView.allowsSelectionDuringEditing = YES; 

然後覆蓋setEditing:動畫:

- (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]; 
    } 
} 
相關問題