2014-02-20 19 views
0

我對Xcode相當陌生,並且在搜索欄中顯示錶格視圖數據的原因時遇到困難。基本上試圖將提取的結果返回到搜索欄。提前致謝!不顯示在SearchBar中的單元--Core數據

#import "DeviceViewController.h" 
#import "DeviceDetailViewController.h" 
@interface DeviceViewController() 
@property (strong) NSMutableArray *devices; 
@end 

@implementation DeviceViewController 
{ 
    NSArray *recipes; 
    NSArray *searchResults; 
} 
@synthesize recipe; 
- (NSManagedObjectContext *)managedObjectContext { 
    NSManagedObjectContext *context = nil; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate performSelector:@selector(managedObjectContext)]) { 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    //label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont fontWithName:@"HelveticaNeue-thin" size:28]; 
    //label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
    label.textColor = [UIColor blackColor]; 
    self.navigationItem.titleView = label; 
    label.text = @"TapNotes"; 
    [label sizeToFit]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"]; 
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    [self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [searchResults count]; 

    } else { 
     return self.devices.count; 
    } 
    //return self.devices.count; 
    } 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } 
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]]; 
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",[device valueForKey:@"version"]]]; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     recipe = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
     [self.devices objectAtIndex:indexPath.row]; 
    } 

    // cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image]; 

    return cell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete object from database 
     [context deleteObject:[self.devices objectAtIndex:indexPath.row]]; 

     NSError *error = nil; 
     if (![context save:&error]) { 
      NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
      return; 
     } 

     // Remove device from table view 
     [self.devices removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    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 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"UpdateDevice"]) { 
     NSManagedObject *selectedDevice = [self.devices objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; 
     DeviceDetailViewController *destViewController = segue.destinationViewController; 
     destViewController.device = selectedDevice; 
    } 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 55; 
} 
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; 
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate]; 
} 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
@end 
+3

顯示您的錯誤日誌...?未聲明的標識符是什麼.......? – Mani

+0

我只是做了一個乾淨的似乎解決了錯誤。但現在當我在搜索欄中搜索時,我的表格視圖中的數據不會顯示。 arg –

+0

嘿,男人?你在調試區域得到的錯誤是什麼?只需發佈它。 – Mani

回答

0

事情看起來不在這裏。

  1. 爲什麼不使用NSFetchedResultsConteroller
  2. 什麼是recipesdevices陣列
  3. 你是不是由devices陣列
  4. 你必須設置食譜陣列(您篩選它filterContentForSearchText:
  5. cellForRowAtIndexPath:細胞始終設置數據之間的連接很多「死」的代碼

它看起來像你從一個示例代碼並修改它,但只有一半。

+0

謝謝丹!你是對的。我在「如何在iOS 7中實現搜索欄」上使用了一個名爲AppCoda的網站。大部分代碼是正確的,死區似乎在代碼似乎沒有響應的cellForRowAtIndexPath,也是NSArray。我應該從哪裏出發?謝謝 –

+0

首先刪除'recipes'變量並更正此修改出現的錯誤。這應該讓你開始。接下來修正你的'cellForRow ...',首先從適當的數組(搜索或設備)獲取你需要的模型,然後設置單元... –

+0

我修復了這些東西,現在我工作了。這意味着我的世界! Cheers brah:3 –

相關問題