请在这里帮助我。我遇到了一些非常奇怪的错误,试图对我的字符串 TableView 对象进行排序。
行为很简单,我有:
CreateViewController - 我在这里创建字符串并将它们添加到核心数据中
StackTableViewController - 这是我创建的字符串的表格 View 。它们应该按日期排序,第一个单元格是最旧的,第二个单元格是在他之后创建的,依此类推......
HomeViewController - 这里我有一个 UILabel,它一直保存表格 View 的第一个对象,如果我在表格中删除它,我会使用委托(delegate)方法更新这个标签,
原来如此。
问题:
如果表格 View 中没有字符串,然后我在创建页面中添加了一个字符串,它会被很好地创建,但是如果添加另一个,新的字符串会在列表的顶部...但是如果我终止该应用程序并再次打开它的顺序很好。
在我添加更多字符串后还有一些其他奇怪的行为......但问题是,每当我终止应用程序并再次打开它时,一切都完美无缺......:/
这是相关代码:
CreateViewController.m
- (id)init {
self = [super initWithNibName"CreateViewController" bundle:nil];
if (self) {
}
return self;
}
- (void)save {
if (self.myTextView.text.length == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle"Hey!" message"You can't save a blank string" delegate:self cancelButtonTitle"Ok" otherButtonTitles:nil, nil];
[alert show];
}
[self insertTeget];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
[self.myTextView resignFirstResponder];
}
//this is where I add the string to the NSManagedObject object I have called 'Target'
- (void)insertTeget {
CoreDataStack *stack = [CoreDataStack defaultStack];
Target *target = [NSEntityDescription insertNewObjectForEntityForName"Target" inManagedObjectContext:stack.managedObjectContext];
if (self.myTextView.text != nil) {
target.body = self.myTextView.text;
}
[stack saveContext];
}
这是表格 View :
StackTableViewController.m
- (id)init {
self = [super initWithNibName"StackTableViewController" bundle:nil];
if (self) {
[self fetchData];
}
return self;
}
//currentTarget is a string I have in the .h file to pass the home view controller the first string of the table view
- (void)fetchData {
[self.fetchedResultController performFetch:nil];
if (self.fetchedResultController.fetchedObjects.count > 0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
self.currentTarget = current.body;
}else {
self.currentTarget = nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self fetchData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
// Return the number of sections.
return self.fetchedResultController.sections.count;
}
- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCellEditingStyle)tableViewUITableView *)tableView editingStyleForRowAtIndexPathNSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//here I'm updating the delegate when A cell was deleted so I can update the label in the home view controller
- (void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath {
Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
CoreDataStack *stack = [CoreDataStack defaultStack];
[[stack managedObjectContext] deleteObject:target] ;
[stack saveContext];
if ([_delegate respondsToSelectorselector(didDeleteObject)]) {
[self fetchData];
[_delegate didDeleteObject];
}
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *cellIdentifier = @"StackTableViewCell";
Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed"StackTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.cellLabel.text = target.body;
return cell;
}
// this is my fetch request where im setting up the sort descriptor
- (NSFetchRequest *)targetsFetchRequest {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"Target"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"time" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
return fetchRequest;
}
- (NSFetchedResultsController *)fetchedResultController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
CoreDataStack *stack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self targetsFetchRequest];
_fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultController.delegate = self;
return _fetchedResultController;
}
这是我更新标签的主视图 Controller :
- (id)init {
self = [super initWithNibName:@"HomeViewController" bundle:nil];
if (self) {
stackTableViewController = [[StackTableViewController alloc] init];
stackTableViewController.delegate = self;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[stackTableViewController fetchData];
self.homeLabel.text = stackTableViewController.currentTarget;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.homeLabel.text = stackTableViewController.currentTarget;
}
//this is the delegate method
- (void)didDeleteObject {
self.homeLabel.text = stackTableViewController.currentTarget;
}
- (IBAction)goToStack:(id)sender {
[self.navigationController pushViewController:stackTableViewController animated:YES];
}
- (IBAction)goToCreate:(id)sender {
CreateViewController *createViewController = [[CreateViewController alloc]initWithNibName:@"CreateViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:createViewController];
[self.navigationController presentViewController:navigationController animated:YES completion:nil]}
拜托拜托,这让我发疯了,为什么只有在我终止应用程序后订单才能正常?
谢谢@@@!
Best Answer-推荐答案 strong>
我没有看到你在任何地方设置时间。当您创建对象时,它应该设置为 [NSDate date] ,如果它充当创建日期。
关于ios - 使用 NSSortDescriptor 按时间错误排序(带有核心数据),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/27863393/
|