向表视图控制器的每个新条目添加新条目 [英] Add new entries to a table view controller at the top of the table with each new entry

查看:107
本文介绍了向表视图控制器的每个新条目添加新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作应用程序,到目前为止,基本上由以下内容组成:



使用Core Data,我有一个添加按钮的表视图控制器,模式调用一个新的视图控制器提示用户将文本添加到三个字段。当用户单击保存时,该条目作为填充信息的字幕单元被添加到表视图控制器。它工作良好。



我困惑的一直在寻找答案的是每个新条目添加到单元格的底部,因此如果有足够的单元格填充屏幕,新添加的条目将添加到底部。我真的想得到每个新的条目到表视图单元格的顶部。



我已经实现了按字母顺序排序描述符,它也工作,但真的,我想要添加新的条目到表视图的顶部,而不考虑起始字符。



我的FetchRequest目前看起来像这样:

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@Transaction]; 
NSSortDescriptor * sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@whoBy.nameascending:YES selector:@selector(localizedCaseInsensitiveCompare :)];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

self.transactions = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

这是将条目添加到数据库的代码:

  NSManagedObject * person = [NSEntityDescription insertNewObjectForEntityForName:@PersoninManagedObjectContext:context]; 
[person setValue:self.nameTextField.text forKey:@name];

[transaction setValue:person forKey:@whoBy];

任何想法都不胜感激。

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription * entityDesc = [NSEntityDescription entityForName:@dateinManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDesc];

[fetchRequest setFetchBatchSize:10];

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@dateascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObjects:sortDescriptor,nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSArray * fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:& error];


I have a working app so far which basically consists of the following:

Using Core Data, I have 1 Table View Controller with an Add Button which modally calls up a new View Controller prompting the user to add text into three fields. When the user clicks save, the entry is added to the table view controller as a subtitle cell with the information filled in. It works well.

What I am confused about and have been looking for answers is the fact that each new entry gets added to the bottom of the cells, so if there enough cells to fill the screen, the new added entry gets added to the bottom. I would really like to get each new entry to the top of the table view cell.

I have implemented the Alphabetical sortDescriptors which also work, but truthfully, I'd want to add new entries to the top of the Table View regardless of starting character.

My FetchRequest currently looks like this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Transaction"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"whoBy.name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

self.transactions = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];

This is the code that adds the entries to the database:

NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
[person setValue:self.nameTextField.text forKey:@"name"];

[transaction setValue:person forKey:@"whoBy"];

Any thoughts would be appreciated!

解决方案

You can also get your answers in Descending by Coredata fetch request.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"date" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDesc];

[fetchRequest setFetchBatchSize:10];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];    

[fetchRequest setSortDescriptors:sortDescriptors];

NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

这篇关于向表视图控制器的每个新条目添加新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆