2017-02-24 35 views
1

一直試圖弄清楚這一點,我不明白爲什麼這會一直崩潰。我刷的細胞和刪除按鈕顯示出來,但是當按下它崩潰的:刪除位於indexPath.row的Firebase數據

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

崩潰的代碼是這樣的:

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

我想刪除在該火力地堡內容被選中刪除的行。任何人都知道我錯過了什麼?請只有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

你爲什麼要混合self.dataRef和_dataRef?爲什麼在tableView:commitEditingStyle方法中定義self.dataRef。你可以在viewDidLoad中定義它並在整個應用程序中重新使用。您是否檢查過* NSString * index = self.finalArray [indexPath.row]; *添加NSLog(@「%@」,index)的結果可能會揭示問題的一部分。 – Jay

+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把數據字典中的火力點數據:

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