2013-05-07 96 views
0

我正在使用「滑動刪除」,如果要刪除序列中的多個單元格,則uitableview中的某些單元格會加倍或者某些已刪除的單元格出現。因爲我不能發表任何圖片,我將描述的行爲表:刪除多個單元格後,uitableview添加了一些行

  1. 表刪除單元前:用戶1,用戶2,用戶3,用戶4,用戶5,添加新的用戶。
  2. 刪除用戶1和用戶5後的表格:用戶2,用戶3,用戶4,添加新用戶,添加新用戶(但不可選)。
  3. 刪除用戶3後的表格:用戶4,用戶2,添加新用戶,用戶5(可選),添加新用戶(不可選)。

方法,其中我刪除單元格的樣子:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.usersTable beginUpdates]; 
     UITableViewCell *cell = (UITableViewCell *)[self.usersTable cellForRowAtIndexPath:indexPath]; 
     NSString *userNameToDelete = cell.textLabel.text; 
     // Data source 
     [self.appDict removeObjectForKey:userNameToDelete]; 
     self.arrayOfUserNames = [[NSMutableArray alloc] initWithArray:[self.appDict allKeys]]; 
     [self.appDict writeToFile:self.pathOfAppFile atomically:YES]; 
     // Deleting cell 
     [self.usersTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.usersTable endUpdates]; 
    } 
} 

法支持編輯:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row == [self.arrayOfUserNames count]) { 
    return NO; 
} 
else { 
    return YES; 
} 

在節中的行數:

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

我已經嘗試[self.usersTable重載],但一切都停留在s AME。我也嘗試在numberOfRowsInSection中更改表的大小:但這也無濟於事。任何想法我做錯了什麼?

回答

0

haven't你實現

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //delete your user record 
    }  
} 

如果不是,Tableview將刪除單元格,但底層數據將不會被修改,並導致這樣一個奇怪的行爲。

編輯:

嘗試使用:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     NSString *userNameToDelete = [[self arrayOfUserNames] objectAtIndex:indexPath.row]; 
     // Data source 
     [self.appDict removeObjectForKey:userNameToDelete]; 
     self.arrayOfUserNames = [[NSMutableArray alloc] initWithArray:[self.appDict allKeys]]; 
     [self.appDict writeToFile:self.pathOfAppFile atomically:YES]; 

     [tableView reloadData]; 


    } 
} 

給一個簡短的說明:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

被調用時,TableCell的是從已刪除tableview,所以你不必處理這個。如果你這樣做,你會刪除下一個單元格。

+0

我將self.usersTable更改爲該方法中的tableView,但行爲保持不變。你有這個想法嗎? – user1165156 2013-05-07 13:09:52

+0

哦對不起。似乎我有點失明;) – 2013-05-07 17:14:28

+0

你不必手動刪除表格。我會編輯我的答案... – 2013-05-07 17:36:35

相關問題