2015-10-15 110 views
0

在這裏我有一個ViewController,頂部有一個搜索欄和它下面的一個tableView,我期望的是tableView顯示所有的東西,當沒有搜索,如果有搜索,搜索的表格將顯示搜索結果。我已經實現了它,但有一些錯誤,這是tableView可以正確顯示的東西,而無需搜索,但是當我搜索時,搜索欄下方的表視圖什麼也沒有顯示,我NSLog out rowInSection,並找到它是正確的,但爲什麼沒有出現? 這裏是代碼,有人可以過去發現錯誤?提前致謝。 (我在TableViewController中做了同樣的事情,它工作,但我切換到ViewController,它不工作)。查看控制器中的搜索欄和搜索顯示控制器在其中有一個UITableView

//MySearchViewController.h 
@interface MySearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (weak, nonatomic) IBOutlet UITableView *myTableView; 


- (void)reloadView; 

@end 


//MySearchViewController.m 
@interface MySearchViewController() 

@property NSUserDefaults *usrDefault; 

@end 

@implementation MySearchViewController { 
    NSMutableArray *events; 
    NSArray *searchResults; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.usrDefault = [NSUserDefaults standardUserDefaults]; 
    events = [[NSMutableArray alloc] init]; 
    [self extractEventArrayData]; 

} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:YES]; 
    [self reloadView]; 
} 

- (void)reloadView { 
    NSLog(@"reloadView"); 
    events = [[NSMutableArray alloc] init]; 
    [self extractEventArrayData]; 
    [self.myTableView reloadData]; 
} 

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

- (void)extractEventArrayData { 
    NSArray *dataArray = [[NSArray alloc] initWithArray:[self.usrDefault objectForKey:@"eventDataArray"]]; 

    for (NSDate *dataObject in dataArray) { 
     MyEventInfo *eventDecodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:dataObject]; 
     [events addObject:eventDecodedObject]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     NSLog(@"searchResults count:%lu",(unsigned long)[searchResults count]); 
     return [searchResults count]; 

    } else { 
     return [events count]; 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 360; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomTableCell"; 
    MyEventTableViewCell *cell = (MyEventTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[MyEventTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Display MyEventTableViewCell in the table cell 
    MyEventInfo *event = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     event = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
     event = [events objectAtIndex:indexPath.row]; 
    } 

    cell.nameOfEvent.text = event.nameOfEvent; 
    cell.imageOfEvent.image = [UIImage imageNamed:event.imageOfEvent]; 
    cell.timeOfEvent.text = event.timeOfEvent; 
    cell.locationOfEvent.text = event.locationOfEvent; 
    cell.dateOfEvent.text = event.dateOfEvent; 

    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"nameOfEvent contains[c] %@", searchText]; 
    searchResults = [events filteredArrayUsingPredicate:resultPredicate]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
+0

您是否記得將tableview.delegate和tableView.dataSource設置爲self? – scord

+0

是的,我確實設置了,並且tableview可以正確顯示,但是當我想要做一些搜索時,搜索控制器的tableview無法正確顯示。 –

+0

您是否設置了UISearchDisplayController以使用搜索欄?你的搜索欄是你的tableview的子視圖嗎? – beyowulf

回答

0

首先,UISearchDisplayController是在IOS 8棄用。你應該使用UISearchController。我相信你使用的是UISearchDisplayController,因爲你在Storyboard中設置了它,Apple沒有更新Storyboard的對象庫來使用UISearchController。但是,您仍然應該轉換爲UISearchController並在代碼中實現所有內容,直到Apple更新故事板對象庫。在代碼中設置非常簡單。

無論如何,對於您當前的問題,我認爲這是由於您沒有設置searchDisplayController的searchResultsTableView的數據源和委託而造成的。

在viewDidLoad中,你應該做

searchDisplayController.searchResultsTableView.delegate = self; 
searchDisplayController.searchResultsTableView.dataSource = self; 

簡單的設置委託和IBOutlet中「myTableView」的數據源,因爲一旦你開始搜索時,searchDisplayController提出了自己的tableView「searchDisplayController不會做的伎倆。 searchResultsTableView「位於當前視圖的頂部。當前視圖控制器需要爲searchResultsTableView的數據源和委託。

希望這可以/幫助!

+0

嗨,謝謝爲你的答案,但我已經嘗試過你說的,它沒有奏效。我遇到的問題是,當我搜索時,出現了一個resultTableView,但是這個新的tableView僅包含空白單元!,並且在控制檯中NSLog記錄了行數,它看起來應該是正確的。我有點無法弄清楚爲什麼這個單元格是空白的。你可以看看我在我的問題中更新的圖片。非常感謝你。 –

+0

很高興你知道了! – Midas

0

謝謝大家,我想通了,因爲我寫了

MyEventTableViewCell *cell = (MyEventTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

,但它應該是這樣的

MyEventTableViewCell *cell = (MyEventTableViewCell *)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

什麼錯誤!

相關問題