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