Question

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!

Was it helpful?

Solution 2

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];

OTHER TIPS

you can insert new record at place by

 [self.tableview insertRowsAtIndexPaths:indexPath withRowAnimation:YES ];

And also you can done it with by updating your array.

You can add object to as:

 [yourArray insertObject:object atIndex:0]; //for first row.

Then reload table with new array :

 [yourTable reloadData]

this will work for you.

I have sorted this issue out for future reference if anyone is reading this.

In the Add Entry View Controller (the view controller that adds the entries to the database and has a save button method), I did the following:

1) I added a property to the class (@property (nonatomic, strong) NSDate *dateToday; 2) In the save method, I did the following:

self.dateToday = [NSDate date]; 
[transaction setValue:self.dateToday forKey:@"createdDate"];
// transaction is looking into the Transaction Entity which has an attribute called createdDate of type Date. 

3) In the TableViewController and with the NSFetchRequest, I did the following:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO]; 
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
[fetchRequest setSortDescriptors:sortDescriptors];

And so now, when I create a new entry, because Ascending is set to NO, the newly created entry is at the top of the Table View Controller.

Thanks for your guidance @Pradhyuman Chavda

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top