删除位于indexPath.row的Firebase数据

时间:2017-02-24 18:35:43

标签: objective-c xcode firebase firebase-realtime-database tableview

试图解决这个问题已经有一段时间了,我无法理解为什么这会一直崩溃。我滑动单元格并显示删除按钮,但是当按下它时会崩溃:

    [[[_datRef child:@"posts"]child:index] removeValue];

崩溃代码是这样的:

***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FIRDataSnapshot length]: unrecognized selector sent to instance 0x61800002a080**

我正在尝试删除选中要删除的行中的Firebase内容。谁知道我错过了什么?请只使用Objective-C。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return finalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"updateCell" forIndexPath:indexPath];
    FIRDataSnapshot *snapshot = (self.finalArray)[indexPath.row];
    NSString *title = snapshot.value[@"title"];
    NSString *description = snapshot.value[@"description"];
    NSString *date = snapshot.value[@"date"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    cell.titleLabel.text = title;
    cell.updateTextView.text = description;
    NSString *timeAgoFormattedDate = [NSDate mysqlDatetimeFormattedAsTimeAgo:date];
    cell.dateLabel.text = timeAgoFormattedDate;
    cell.updateTextView.delegate = self;
    cell.clipsToBounds = YES;
    return cell;
}


- (void)getUpdates {
    posts = [_datRef child:@"posts"];
    [[posts queryOrderedByChild:@"date"] observeEventType:FIRDataEventTypeValue
                                                withBlock:^(FIRDataSnapshot *snapshot) {
                                                  self.updatesArray = [NSMutableArray array];
                                                  for (snapshot in snapshot.children) {
                                                      [self.updatesArray addObject:snapshot];
                                                      _sortArray = [updatesArray reverseObjectEnumerator].allObjects;
                                                      self.finalArray = [NSMutableArray array];
                                                      [self.finalArray addObjectsFromArray:_sortArray];
                                                  }
                                                  [self.tableView reloadData];
                                                }];

    [self.tableView reloadData];
}

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[_datRef child:@"posts"] removeValue];
        [finalArray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

1 个答案:

答案 0 :(得分:0)

我找到了一种更简单的方法。在你的帖子方法中:

//Create a random string for you child:
NSString *uuid = [NSUUID UUID].UUIDString;

//Put the random string as one of your childs:
[[[_alertRef child:@"posts"] child:uuid] setValue:@{ 

//create a key called childID and assign it the same value:
@"childID" : uuid, 
@"date" : _date, 
@"description" : _descriptionTextView.text, 
@"title" : _alertTitle.text }];

现在,当您在tableView中检索firebase数据时,将数据放入字典中:

- (void)getUpdates {
    posts = [_datRef child:@"posts"];
    [[posts queryOrderedByChild:@"date"] observeEventType:FIRDataEventTypeValue
                                                withBlock:^(FIRDataSnapshot *snapshot) {
                                                  self.updatesArray = [NSMutableArray array];
                                                  for (snapshot in snapshot.children) {
                                                      [self.updatesArray addObject:snapshot];
                                                      _firebaseDict = [[NSDictionary alloc] init];
                                                      _firebaseDict = snapshot.value;
                                                      _sortArray = [updatesArray reverseObjectEnumerator].allObjects;
                                                      self.finalArray = [NSMutableArray array];
                                                      [self.finalArray addObjectsFromArray:_sortArray];
                                                  }
                                                  [self.tableView reloadData];
                                                }];
    [self.tableView reloadData];
}

然后在你的tableView编辑方法中:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *childID = [_firebaseDict valueForKey:@"childID"];
        [[[_datRef child:@"posts"] child:childID] removeValue];
        [finalArray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    }
}