返回时,表视图中已删除的行重新出现

Deleted row in tableview is reappearing when navigating back

有 friendsviewcontroller,其中有 uibarbuttonItem 用于编辑好友列表,其他 uibarbuttonitem 用于为群聊创建群组。

有多个 segue 用于切换视图控制器。

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Groups";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"New" style:UIBarButtonItemStylePlain target:self
                                                                         action:@selector(actionNew)];
self.tableView.separatorInset = UIEdgeInsetsZero;
chatrooms = [[NSMutableArray alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([PFUser currentUser] != nil)
{
    [self refreshTable];
}
else LoginUser(self);
}

#pragma mark - User actions
- (void)actionNew
 {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Create New Group" message:nil delegate:self
                                      cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
 }

#pragma mark - UIAlertViewDelegate
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:    (NSInteger)buttonIndex=
 {
if (buttonIndex != alertView.cancelButtonIndex)
{
    UITextField *textField = [alertView textFieldAtIndex:0];
    if ([textField.text isEqualToString:@""] == NO)
    {
        PFObject *object = [PFObject objectWithClassName:PF_CHATROOMS_CLASS_NAME];
        object[PF_CHATROOMS_NAME] = textField.text;
        [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
        {
            if (error == nil)
            {
                [self refreshTable];
            }
            else [ProgressHUD showError:@"Network error."];
        }];
    }
}
 }
- (void)refreshTable
 {
[ProgressHUD show:nil];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    if (error == nil)
    {
        [chatrooms removeAllObjects];
        for (PFObject *object in objects)
        {
            [chatrooms addObject:object];
        }
        [ProgressHUD dismiss];
        [self.tableView reloadData];
    }
    else [ProgressHUD showError:@"Network error."];
}];
 }

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

PFObject *chatroom = chatrooms[indexPath.row];
cell.textLabel.text = chatroom[PF_CHATROOMS_NAME];

return cell;
 }

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

if (editingStyle == UITableViewCellEditingStyleDelete) {
    PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
    [chatrooms removeObjectAtIndex:chatroom];
    //[chatrooms removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    
}
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFObject *chatroom = chatrooms[indexPath.row];
NSString *roomId = chatroom.objectId;
CreateMessageItem([PFUser currentUser], roomId, chatroom[PF_CHATROOMS_NAME]);
ChatView *chatView = [[ChatView alloc] initWith:roomId];
chatView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatView animated:YES];
 }

导航回 TableView 时,table 视图中删除的行重新出现

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

if (editingStyle == UITableViewCellEditingStyleDelete) {
    PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
    [chatrooms removeObjectAtIndex:chatroom];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];   
}
}

我找不到我遗漏了哪一段代码或者我到底做错了什么。

如果有人可以指出这一点。

非常感谢。

谢谢。

提交删除时,您的代码从 chatrooms 中删除对象,在您的案例中这是 table 视图的数据源,但这发生在您的应用内存中,即 源从中填充 chatrooms 不会更改。因此,在 MVC 中,应用程序的模型状态不会在视图状态更改后更新。

每次显示 table 视图时,您的代码会在 refreshTable 中填充 chatrooms,如果模型的状态未更改,代码将获得与之前相同的列表,因此 table 视图不会改变。

编辑: 而不是使用另一种方法来刷新 table 视图,您需要考虑您的应用程序在此 table 视图中做了什么.如果用户可以删除 table 视图中的内容,您的应用程序是否也应该更新模型(该模型可以是本地或远程数据库、属性 列表文件等)?如果是,则当用户在 table 视图中插入或删除行时更新模型;好吧,如果没有,那么你问的问题不是问题,或者 table 视图应该关闭编辑。

编辑1:

如果您确实需要更新数据,根据您的代码,您可能需要这样做:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
        [chatrooms removeObjectAtIndex:indexPath.row];
        PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
        [query deleteChatroom:chatroom];
        [tableView deleteRowsAtIndexPaths:@[indexPath] 
                         withRowAnimation:UITableViewRowAnimationAutomatic];    
    }
}

也就是说,您可能需要实现 class PFQuery 的方法 deleteChatroom: