2017-02-24 4 views
1

これをしばらく理解しようとしていましたが、なぜこれがクラッシュするのか理解できません。私は、セルをスワイプし、削除ボタンが現れますが、押されたとき、それは時にクラッシュ:indexPath.rowにあるFirebaseデータを削除

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

クラッシュコードはこれです:私は、Firebaseコンテンツを削除しようとしている

***** 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を混ぜていますか?なぜself.dataRefがtableView:commitEditingStyleメソッドの内部で定義されているのですか?これをviewDidLoadで定義し、アプリ全体で再利用することができます。 * NSString * index = self.finalArray [indexPath.row]; * NSLog(@ "%@"、インデックス)を追加すると、問題の一部が明らかになる可能性があります。 – Jay

+0

私の悪い、それを修正する –

答えて

0

私はこれを行う簡単な方法を見つけました。あなたの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]; 
} 

- (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]; 
    } 
} 
今、あなたが辞書にデータを入れて、あなたのtableViewであなたのfirebaseデータを取得するために行くとき

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

:あなたのPOSTメソッドで

関連する問題