Tôi đã thử in ra bao nhiêu hàng và phần thực tế trong tableView trong quá trình cập nhật khiến tôi suy nghĩ, có bao nhiêu mục tôi quay lại trong chế độ xem bảng nguồn dữ liệu ... và đây là thủ phạm :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 0;
}
Hãy chắc chắn rằng bạn đang trở về 1 và không 0.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
ít nhất này đã giải quyết được vấn đề đối với tôi ...
CẬP NHẬT:
Tôi gặp vấn đề tương tự một lần nữa sau khi hoạt động được một thời gian. Tôi đã sửa đổi ứng dụng một chút. Quan điểm đầu tiên của tôi là một tableView và khi bạn nhấp vào nút dấu cộng ở phía trên bên phải, bạn sẽ có được một bảng khác, nơi bạn có thể nhập thông tin. Khi bạn nhấp vào thực hiện, thông tin được thêm vào mô hình Core Data với mã này:
- (IBAction)doneButtonPressed:(UIBarButtonItem *)sender
{
MoneyBadgeAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newMoment;
newMoment = [NSEntityDescription
insertNewObjectForEntityForName:@"SpendingMoment"
inManagedObjectContext:context];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *modifiedDate = [dateFormat dateFromString: self.dateTextField.text];
[newMoment setValue: modifiedDate forKey:@"date"];
[newMoment setValue: descTextField.text forKey:@"desc"];
[appDelegate.eventsArray insertObject:newMoment atIndex:0];
NSError *error;
[context save:&error];
[self dismissViewControllerAnimated:YES completion:nil];
}
Và tôi khẳng định nó được đưa vào mô hình Core Data thành công bằng cách in ra cửa sổ Console sau trong phương pháp viewDidAppear của tôi khi trở về trở lại màn hình đầu tiên.
-(void)viewDidAppear:(BOOL)animated{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpendingMoment"
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
for (SpendingMoment *moment in items) {
NSLog(@"Date: %@, Desc: %@", [moment date], [moment desc]);
}
[self addEvent];
Và sau đó phương pháp addEvent tôi thực hiện điều này:
- (void)addEvent {
[self.tableScreen beginUpdates];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableScreen insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableScreen reloadData];
[self.tableScreen endUpdates];
@try{
[self.tableScreen scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
@catch(NSException *exception){
}
}
Bất kỳ ý tưởng gì vấn đề là thời gian này ??? Cảm ơn!
tôi đã làm điều đó .. [timeZoneName insertObject: @ "HELLO" atIndex: 0]; số hàng luôn hơn 1. Tôi vẫn gặp lỗi này .. – insomiac
kiểu trả về của sốOfRowsInSection là [timeZoneNames count]; Bạn có sử dụng insertRowsAtIndexPaths không cần startUpdates và endUpdates không ?? – insomiac
@Aby Từ tài liệu '-beginUpdates':" Gọi phương thức này nếu bạn muốn các thao tác chèn, xóa và chọn tiếp theo (ví dụ: cellForRowAtIndexPath: và indexPathsForVisibleRows) được làm động cùng lúc. " Nó được sử dụng để nhóm nhiều hoạt động UI vào một hoạt động UI đơn lẻ: bạn chỉ có một thao tác. –