ios - 使用 NSSortDescriptor 按时间错误排序(带有核心数据)
<p><p>请在这里帮助我。我遇到了一些非常奇怪的错误,试图对我的字符串 TableView 对象进行排序。</p>
<p>行为很简单,我有:</p>
<p>CreateViewController - 我在这里创建字符串并将它们添加到核心数据中</p>
<p>StackTableViewController - 这是我创建的字符串的表格 View 。它们应该按日期排序,第一个单元格是最旧的,第二个单元格是在他之后创建的,依此类推......</p>
<p>HomeViewController - 这里我有一个 UILabel,它一直保存表格 View 的第一个对象,如果我在表格中删除它,我会使用委托(delegate)方法更新这个标签,</p>
<p>原来如此。</p>
<p>问题:</p>
<p>如果表格 View 中没有字符串,然后我在创建页面中添加了一个字符串,它会被很好地创建,但是如果添加另一个,新的字符串会在列表的顶部...但是如果我终止该应用程序并再次打开它的顺序很好。</p>
<p>在我添加更多字符串后还有一些其他奇怪的行为......但问题是,每当我终止应用程序并再次打开它时,一切都完美无缺......:/</p>
<p>这是相关代码:</p>
<p>CreateViewController.m</p>
<pre><code>- (id)init {
self = ;
if (self) {
}
return self;
}
- (void)save {
if (self.myTextView.text.length == 0) {
UIAlertView *alert = [ initWithTitle:@"Hey!" message:@"You can't save a blank string" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
;
}
;
;
;
}
//this is where I add the string to the NSManagedObject object I have called 'Target'
- (void)insertTeget {
CoreDataStack *stack = ;
Target *target = ;
if (self.myTextView.text != nil) {
target.body = self.myTextView.text;
}
;
}
</code></pre>
<p>这是表格 View :</p>
<p>StackTableViewController.m</p>
<pre><code>- (id)init {
self = ;
if (self) {
;
}
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 {
;
if (self.fetchedResultController.fetchedObjects.count > 0) {
NSIndexPath *indexPath = ;
Target *current = ;
self.currentTarget = current.body;
}else {
self.currentTarget = nil;
}
}
- (void)viewDidLoad {
;
;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return self.fetchedResultController.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = ;
return ;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)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)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Target *target = ;
CoreDataStack *stack = ;
[ deleteObject:target] ;
;
if () {
;
;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"StackTableViewCell";
Target *target = ;
StackTableViewCell *cell = ;
if (!cell)
{
NSArray *topLevelObjects = [ loadNibNamed:@"StackTableViewCell" owner:self options:nil];
cell = ;
}
cell.cellLabel.text = target.body;
return cell;
}
// this is my fetch request where im setting up the sort descriptor
- (NSFetchRequest *)targetsFetchRequest {
NSFetchRequest *fetchRequest = ;
NSSortDescriptor *sortDescriptor = [ initWithKey:@"time" ascending:YES];
NSArray *sortDescriptors = [ initWithObjects:sortDescriptor, nil];
;
return fetchRequest;
}
- (NSFetchedResultsController *)fetchedResultController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
CoreDataStack *stack = ;
NSFetchRequest *fetchRequest = ;
_fetchedResultController = [ initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultController.delegate = self;
return _fetchedResultController;
}
</code></pre>
<p>这是我更新标签的主视图 Controller :</p>
<pre><code>- (id)init {
self = ;
if (self) {
stackTableViewController = [ init];
stackTableViewController.delegate = self;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
;
self.homeLabel.text = stackTableViewController.currentTarget;
}
- (void)viewDidLoad {
;
self.homeLabel.text = stackTableViewController.currentTarget;
}
//this is the delegate method
- (void)didDeleteObject {
self.homeLabel.text = stackTableViewController.currentTarget;
}
- (IBAction)goToStack:(id)sender {
;
}
- (IBAction)goToCreate:(id)sender {
CreateViewController *createViewController = [initWithNibName:@"CreateViewController" bundle:nil];
UINavigationController *navigationController = [initWithRootViewController:createViewController];
}
</code></pre>
<p>拜托拜托,这让我发疯了,为什么只有在我终止应用程序后订单才能正常?
谢谢@@@!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我没有看到你在任何地方设置时间。当您创建对象时,它应该设置为 <code></code>,如果它充当创建日期。</p></p>
<p style="font-size: 20px;">关于ios - 使用 NSSortDescriptor 按时间错误排序(带有核心数据),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27863393/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27863393/
</a>
</p>
页:
[1]