2013-09-30 7 views
0

Пока я обновлял свое приложение для iOS7, у меня возникла проблема с одним из моих UITableViews, к которым привязан SearchBar. Я планировал использовать UIViewController, содержащий TableView и SearchBar, в моем обновлении, но я не могу понять, как его запустить в этой комбинации.Проблема с UISearchBar в UIViewController, содержащая TableView

Это мой .h файл

#import <UIKit/UIKit.h> 

@interface lsoNewKE_Table : UIViewController <UISearchDisplayDelegate, UISearchBarDelegate, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate> { 
    NSMutableArray * filteredListContent; 
    NSMutableArray * filteredList; 
    BOOL isSearchContent; 

@private 
    NSFetchedResultsController *fetchedResultsController__; 
    NSManagedObjectContext *managedObjectContext__; 
} 
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController; 
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (strong, nonatomic) NSMutableArray *keTableData; 
@property (nonatomic, retain) NSMutableArray *filteredListContent; 
@property (nonatomic, retain) NSMutableArray *filteredList; 
@property (strong, nonatomic) IBOutlet UITableView *tableView; 

- (void)readDataForTable; 
@end 

Это файл .m

#import "lsoNewKE_Table.h" 
#import "TblKE.h" 
#import "CoreDataHelper.h" 
#import "lsoAppDelegate.h" 
// #import "lsoDetailViewKE.h" 
#import "lsoTableViewCell.h" 

@interface lsoNewKE_Table() 

@end 

@implementation lsoNewKE_Table 

@synthesize keTableData, managedObjectContext=managedObjectContext__, filteredListContent, filteredList, tableView, fetchedResultsController=fetchedResultsController__; 

- (void)reloadFetchedResults:(NSNotification*)note { 
    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    } 

    if (note) { 
     NSLog(@"reloadFetchedResults"); 
     [self readDataForTable]; 
    } 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.keTableData count]]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadFetchedResults:) name:@"RefreshAllViews" object:[[UIApplication sharedApplication] delegate]]; 

    [super viewDidLoad]; 
    UIColor* bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"amazonas.png"]]; 
    [self.view setBackgroundColor:bgColor]; 

    [self readDataForTable]; 
    isSearchContent = FALSE; 
    self.searchDisplayController.searchBar.delegate = self; 
    self.tableView.tableHeaderView = self.searchDisplayController.searchBar; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    // Repopulate the array with new table data 
    [self readDataForTable]; 
} 

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

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    self.filteredListContent = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)readDataForTable 
{ 
    lsoAppDelegate *appDelegate = (lsoAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    // Grab the data 
    keTableData = [CoreDataHelper getObjectsForEntity:@"TblKE" withSortKey:@"desc" andSortAscending:YES andContext:appDelegate.managedObjectContext]; 

    // Force table refresh 
    [self.tableView reloadData]; 
} 

#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 (self.tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     return [self.filteredListContent count]; 
    } 
    else 
    { 
     return [keTableData count]; 
    } 
} 

// Create/reuse a table cell and configure it for display 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCellKE"; 

    lsoTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects; 

     topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellKE_iPhone" owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 

       cell = (lsoTableViewCell *) currentObject; 
      } 
     } 
    } 

    TblKE *currentCell; 

    // Get the core data object we need to use to populate this table cell 
    if (self.tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     self.tableView.backgroundColor = [UIColor blackColor]; 
     currentCell = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     currentCell = [keTableData objectAtIndex:indexPath.row]; 
    } 

    // Fill in the cell contents 
    cell.textLabel.text = [currentCell desc]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Gewicht: %@ - KE: %.1f", [currentCell gewicht], [[currentCell ke]doubleValue]]; 

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 
    cell.imageView.image = [UIImage imageWithData:[currentCell picture]]; 

    return cell; 
} 

// Swipe to delete has been used. Remove the table item 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     lsoAppDelegate *appDelegate = (lsoAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     // Get a reference to the table item in our data array 
     TblKE *itemToDelete = [self.keTableData objectAtIndex:indexPath.row]; 

     // Delete the item in Core Data 
     [appDelegate.managedObjectContext deleteObject:itemToDelete]; 

     // Remove the item from our array 
     [keTableData removeObjectAtIndex:indexPath.row]; 

     // Commit the deletion in core data 
     NSError *error; 
     if (![appDelegate.managedObjectContext save:&error]) 
      NSLog(@"Failed to delete Entry item with error: %@", [error domain]); 

     // Delete the row from the data source 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (isSearchContent == TRUE){ 
     [self performSegueWithIdentifier:@"EditPicture" sender:self]; 
    } else { 
     [self performSegueWithIdentifier:@"EditPicture" sender:nil]; 
    } 
} 

#pragma mark - 
#pragma mark Content Filtering 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    /* 
    Update the filtered array based on the search text and scope. 
    */ 

    [self.filteredListContent removeAllObjects]; // First clear the filtered array. 

    /* 
    Search the main list for products whose name matches searchText; add items that match to the filtered array. 
    */ 
    for (TblKE *itemToSearch in keTableData) 
    { 
     NSComparisonResult result = [itemToSearch.desc compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame) 
     { 
      [self.filteredListContent addObject:itemToSearch]; 
      isSearchContent = TRUE; 
     } 
    } 
} 

#pragma mark - 
#pragma mark UISearchDisplayController Delegate Methods 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString scope:nil]; 

    return YES; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    isSearchContent = FALSE; 
} 

#pragma mark - 
#pragma mark Fetched results controller 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    lsoAppDelegate *appDelegate = (lsoAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (fetchedResultsController__ != nil) 
    { 
     return fetchedResultsController__; 
    } 

    /* 
    Set up the fetched results controller. 
    */ 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TblKE" inManagedObjectContext:appDelegate.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:25]; 

    // Edit the sort key as appropriate. 
    NSSortDescriptor *sortDescriptorName = [[NSSortDescriptor alloc] initWithKey:@"desc" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorName, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 
    aFetchedResultsController.delegate = self; 

    self.fetchedResultsController = aFetchedResultsController; 

    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return fetchedResultsController__; 
} 

@end 

ViewControllerSearchBarSearch Display Controller
Любая помощь будет очень ценна.

Благодаря Ингемар

К сожалению Стюарт, вы правы, следует указать мой вопрос ...

SearchBar прибудет-х перепутался при вводе поискового запроса, что бы я ни ввести в поле он всегда придумывает как это:
messed up SearchBar

я поставил точку останова в cellForRowAtIndexPath в следующем положении, но он никогда не врезался в него.

if (self.tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     self.tableView.backgroundColor = [UIColor blackColor]; 
     currentCell = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     currentCell = [keTableData objectAtIndex:indexPath.row]; 
    } 
+0

Так что же не работает? – StuartM

+0

отредактировал мое сообщение. –

ответ

1

Похож на два полупрозрачных стола. Попробуйте установить background-colour на searchtableview в

(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller; 
+0

Действительно, это сработало и решило одну из проблем, но я все еще сталкиваюсь с проблемой, которую SearchResultsTableView не обновляет в соответствии с тем, что я вхожу в поле поиска. –

+0

все еще задается вопросом, почему self.tableView == self.searchDisplayController никогда не получает вызов '- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) раздел {if (self.tableView == self.searchDisplayController.searchResultsTableView) {NSLog (@ "Результаты возврата результатов ..."); return [self.filteredListContent count]; } else {return [keTableData count]; }} ' –

Смежные вопросы