2012-11-13 56 views
1

我嘗試爲iPhone創建一個簡單的應用程序。viewcontroller中的Searchbar(uitableview)

其中我有一個tableview和tableview的detailview,最後一個搜索欄來過濾tableview。

tableview和detailview工作正常,但searchhbar dos不能正常工作。

當我鍵入一個搜索詞它顯示整個tableview和detailview dos不工作 搜索模式。

當我取消搜索模式的細節視圖再次工作。

因此,我的過濾代碼是錯誤的,我沒有得到我的過濾器代碼中的detailview,但我現在不怎麼做正確。

希望有人能幫助我,我的代碼如下所示:

我的* .h文件:

#import <UIKit/UIKit.h> 

@class DetailViewController; 

@interface MasterViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> 

{ 
    NSArray *originalData; 
    NSMutableArray *searchData; 

    UISearchBar *searchBar; 
    UISearchDisplayController *searchDisplayController; 

} 

@property (strong, nonatomic) DetailViewController *detailViewController; 

@end 

我的* .m文件:

#import "MasterViewController.h" 

#import "DetailViewController.h" 

@interface MasterViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation MasterViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"Opslag", @"Opslag"); 
     _objects = [[NSMutableArray alloc] init]; 

     //title 
     //detail 

     NSDictionary * obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"this_title", @"title", @"this_detail", @"detail", nil]; 
     NSDictionary * obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title2", @"title", @"some_detail2", @"detail", nil]; 
     NSDictionary * obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title3", @"title", @"some_detail3", @"detail", nil]; 
     NSDictionary * obj4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title4", @"title", @"some_detail4", @"detail", nil]; 

     [_objects addObject:obj1]; 
     [_objects addObject:obj2]; 
     [_objects addObject:obj3]; 
     [_objects addObject:obj4]; 

     originalData = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil]; 
     searchData = [[NSMutableArray alloc] init]; 


    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)]; 
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 

    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 

    self.tableView.tableHeaderView = searchBar; 

} 

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



#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _objects.count; 
} 

// Customize the appearance of table view cells. 
- (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]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    cell.detailTextLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"detail"]; 




    return cell; 
} 

#pragma mark - searchDisplayControllerDelegate 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [searchData removeAllObjects]; 

    NSArray *group; 

    for(group in originalData) 
    { 
     NSMutableArray *newGroup = [[NSMutableArray alloc] init]; 
     NSString *element; 

     for(element in group) 
     { 
      NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch]; 

      if (range.length > 0) { 
       [newGroup addObject:element]; 
      } 
     } 

     if ([newGroup count] > 0) { 
      [searchData addObject:newGroup]; 
     } 


    } 

    return YES; 
} 


- (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 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!self.detailViewController) { 
     self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    } 
    NSDictionary *object = _objects[indexPath.row]; 
    self.detailViewController.detailItem = [object objectForKey:@"detail"]; 
    self.detailViewController.titleItem = [object objectForKey:@"title"]; 
    [self.navigationController pushViewController:self.detailViewController animated:YES]; 
} 

@end 

回答

0

cellForRowAtIndexPath方法始終從您的_objects陣列中選取,在執行搜索時您永遠不會改變。當您調用shouldReloadTableForSearchString時,您只會填入searchData,這絕不會反饋回cellForRowAtIndexPathnumberOfRowsInSection以顯示過濾結果。來處理

的一種方法是在你的cellForRowAtIndexPath做這樣的事情(未經測試):

- (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]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSArray* sourceArray = tableView == self.searchDisplayController.searchResultsTableView ? searchData : _objects; 

    cell.textLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    cell.detailTextLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"detail"]; 

    return cell; 
} 

,並在您的numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView){ 
     return searchData.count; 
    }else 
    { 
     return _objects.count; 
    } 

} 
+0

嗨foggzilla,感謝您的回答..我對這個有點新,所以當你寫「使用searchData和_objects數組來填充你的單元格」時,它就是這樣的代碼: cell = [self.searchData objectAtIndex:indexPath.row]; } else cell = [self._objects objectAtIndex:indexPath.row]; ?... –

+0

我在我的答案中更新了'cellForRowAtIndexPath'方法來澄清,但是這就是我所掌握的。 – foggzilla

+0

對不起foggzilla,但我想你讓我失去了那裏......我做了一個NSArray的_object?所以代碼如下所示: if(tableView == self.searchDisplayController.searchResultsTableView) { //使用searchData來填充您的單元格 NSArray * _objects = [self.searchData objectAtIndex:indexPath.row]; } else //使用_objects數組填充單元格 NSArray * _objects = [self._objects objectAtIndex:indexPath.row]; } ? –

相關問題