-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
APLEvent *event = (self.eventsArray)[indexPath.row];
APLEventDetailViewController *inspector = (APLEventDetailViewController *)[segue destinationViewController];
inspector.event = event;
}
这里用了storyboard的destionViewcontroller(segue),可以将storyboard中连线的下一个目标viewcontroller在代码中添加跳转信息。这里为每行的跳转even都分开定义好,因此可以做到在storyboard中一个跳转页面但能实现创建多个。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path.
NSManagedObject *eventToDelete = (self.eventsArray)[indexPath.row];
[self.managedObjectContext deleteObject:eventToDelete];
// Update the array and table view.
[self.eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
这是删除选中row的代码,分为在tableview中删除,和在数据中删除;
这里的@[indexPath] 把后者转换成(NSArray *)类型;
刚开始不是很明白,问了才知道,原来这是数组的初始化。是ios6之后引入的方式。mark下。
@[] 初始化不可变数组
@{} 初始化不可变字典
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Start the location manager.
[[self locationManager] startUpdatingLocation];
/*
Fetch existing events.
Create a fetch request, add a sort descriptor, then execute the fetch.
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
// Set self's events array to the mutable array, then clean up.
[self setEventsArray:mutableFetchResults];
}
具体的codedata实现~