-(void)updateMyLocation{
CLLocation *location = [locationManager location];
if (!location) {
return;
}
// Create and configure a new instance of the Event entity.
Event *event = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
// Configure the new event with information from the location.
CLLocationCoordinate2D coordinate = [location coordinate];
[event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
[event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
[event setDate:[NSDate date]];
NSUInteger randomIndex = arc4random()% [pinArray count];
NSString *buffer = pinArray[randomIndex];
[event setIcon:buffer];
// Commit the change.
NSError *erro = nil;
if (![managedObjectContext save:&erro]) {
//Handle the error.
NSLog(@"save fail");
}
[locationArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView endEditing:YES];
// Delete the managed object at the given index path.
// Delete the row from the data source
NSManagedObject *eventToDelete = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
eventToDelete = [filteredArray objectAtIndex:indexPath.row];
[filteredArray removeObjectAtIndex:indexPath.row];
}
else{
eventToDelete = [locationArray objectAtIndex:indexPath.row];
[locationArray removeObjectAtIndex:indexPath.row];
}
[managedObjectContext deleteObject:eventToDelete];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// Commit the change.
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
CGPoint point = [textField center];
point = [self.tableView convertPoint:point fromView:textField];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
Event *event = locationArray[indexPath.row];
if (textField.text) {
event.address = textField.text;
}
// Commit the change.
NSError *erro = nil;
if (![managedObjectContext save:&erro]) {
//Handle the error.
NSLog(@"save fail");
}
[self.tableView reloadData];
return YES;
}
- (void)viewDidLoad
{
addButton.enabled = NO;
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
if (!mutableFetchedResults) {
locationArray = mutableFetchedResults;
newBounds.origin.y = newBounds.origin.y + locationSearchBar.bounds.size.height;
[[self tableView] setBounds:newBounds];
}
- (void)updateFilteredContentForSearchString:(NSString *)searchString productType:(NSString *)type
{
if (strippedStr.length > 0)
{
searchItems = [strippedStr componentsSeparatedByString:@" "];
}
NSMutableArray *andMatchPredicates = [NSMutableArray array];
for (NSString *searchString in searchItems)
{
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
lhs = [NSExpression expressionForKeyPath:@"yearIntroduced"];
rhs = [NSExpression expressionForConstantValue:targetNumber];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
rhs = [NSExpression expressionForConstantValue:targetNumber];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
}
[andMatchPredicates addObject:orMatchPredicates];
}
NSCompoundPredicate *finalCompoundPredicate = nil;
if (type != nil)
{
if (andMatchPredicates.count > 0)
{
NSCompoundPredicate *compPredicate1 =
(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
NSPredicate *compPredicate2 = [NSPredicate predicateWithFormat:@"(SELF.type == %@)", type];
finalCompoundPredicate =
(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:@[compPredicate1, compPredicate2]];
}
else
{
(NSCompoundPredicate *)[NSPredicate predicateWithFormat:@"(SELF.type == %@)", type];
}
}
else
{
(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
}
self.searchResults = [[self.searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Set the title.
self.title = @"Pins";
pinArray = @[@"pin.png",@"pin1.png",@"pin2.png"];
// Start the location manager.
[self locationManager];
[locationManager startUpdatingLocation];
//fetchRequest
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Event"];
//sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:NO];NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
//Execute the Request
NSError *error = nil;
managedObjectContext = [self managedObjectContext];
NSMutableArray *mutableFetchedResults = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];if (!mutableFetchedResults) {
// Handle the error.
NSLog(@"array none");
}
//search
filteredArray = [NSMutableArray arrayWithCapacity:[locationArray count]];
// Don't show the scope bar or cancel button until editing begins
[locationSearchBar setShowsScopeBar:NO];
[locationSearchBar sizeToFit];
// Hide the search bar until user scrolls up
CGRect newBounds = [[self tableView] bounds];newBounds.origin.y = newBounds.origin.y + locationSearchBar.bounds.size.height;
[[self tableView] setBounds:newBounds];
}
#pragma mark - Content Filtering
{
// start out with the entire list
self.searchResults = [self.products mutableCopy];
// strip out all the leading and trailing spaces
NSString *strippedStr = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// break up the search terms (separated by spaces)
NSArray *searchItems = nil;if (strippedStr.length > 0)
{
searchItems = [strippedStr componentsSeparatedByString:@" "];
}
// build all the "AND" expressions for each value in the searchString
//NSMutableArray *andMatchPredicates = [NSMutableArray array];
{
// each searchString creates an OR predicate for: name, yearIntroduced, introPrice
//
// example if searchItems contains "iphone 599 2007":
// name CONTAINS[c] "iphone"
// name CONTAINS[c] "599", yearIntroduced ==[c] 599, introPrice ==[c] 599
// name CONTAINS[c] "2007", yearIntroduced ==[c] 2007, introPrice ==[c] 2007
//NSMutableArray *searchItemsPredicate = [NSMutableArray array];
// name field matching
NSExpression *lhs = [NSExpression expressionForKeyPath:@"name"];
NSExpression *rhs = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhsrightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// yearIntroduced field matching
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *targetNumber = [numFormatter numberFromString:searchString];
if (targetNumber != nil) // (searchString may not convert to a number)
{lhs = [NSExpression expressionForKeyPath:@"yearIntroduced"];
rhs = [NSExpression expressionForConstantValue:targetNumber];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSEqualToPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// price field matching
lhs = [NSExpression expressionForKeyPath:@"introPrice"];rhs = [NSExpression expressionForConstantValue:targetNumber];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSEqualToPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];}
// at this OR predicate to our master AND predicate
NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];[andMatchPredicates addObject:orMatchPredicates];
}
{
// we have a scope type to narrow our search further
//if (andMatchPredicates.count > 0)
{
// we have a scope type and other fields to search on -
// so match up the fields of the Product object AND its product type
//NSCompoundPredicate *compPredicate1 =
(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
NSPredicate *compPredicate2 = [NSPredicate predicateWithFormat:@"(SELF.type == %@)", type];
(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:@[compPredicate1, compPredicate2]];
}
else
{
// match up by product scope type only
finalCompoundPredicate =(NSCompoundPredicate *)[NSPredicate predicateWithFormat:@"(SELF.type == %@)", type];
}
}
else
{
// no scope type specified, just match up the fields of the Product object
finalCompoundPredicate =(NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
}
}
コメント
コメントを投稿