2010-01-06 1 views
0

조회수 : 아이폰 - jQuery과 문제는 - 데이터를 다시로드 & 더 나는 애플의 샘플 코드의 예를 사용하고

http://developer.apple.com/iPhone/library/samplecode/TableSearch/index.html

이 예에서 jQuery과 시작시 내용의 목록을 얻을. UISearchBar를 클릭하고 입력하면 내용 목록이 필터링되고 ScopeBar의 범위를 확인합니다.

"일반 검색"에 대한이 종류의 "즉시 검색"을 다시 작성해야합니다. 처음에는 TableView에 대한 데이터가 없습니다. 사용자는 SearchBar를 클릭하고, 무언가를 입력하고, "Search"버튼을 누르면 검색 요청이 웹 서버로 전송됩니다. 웹 서버의 응답은 TableView에 입력되며 사용자는 범위를 전환하여 결과 집합을 필터링 할 수 있습니다. 검색 막대의 값을 변경해도 결과 목록은 필터링되지 않습니다. '검색'을 누르면 검색 요청이 시작됩니다.

예제 코드를 가져 와서 다시 작성했습니다 (하단의 소스 코드). 하지만 두 가지 문제가 있습니다.

  1. SearchViewController (TabBar, SearchBar, ScopeBar, TableView)를 처음 호출 할 때 모든 사항이 정상입니다. 거기에 빈 TableView가 있습니다. 그러나 SearchBar를 클릭하고 하나의 문자를 입력하면 "안타 없음"이라는 메시지가 나타납니다. 내가 어떻게 피할 수 있니? 이 메시지는 사용자가 '검색'을 누르고 실제로 일치하는 항목이없는 경우에만 표시됩니다.
  2. 두 번째 문제 : "hello"를 입력하고 "Search"를 누르면 TableView가 결과를 나열하지 않습니다. '중단'을 클릭하거나 다른 범위에서 클릭하면 결과가 나열됩니다. 누락 된 "다시로드"와 같은 것이 있어야합니다!

누군가 나를 도울 수 있기를 바랍니다. 고마워요. & 고맙습니다.

내 소스 코드 : 첫 번째 문제는 검색 창에 대한 대리자를 설정 한 다음 – searchBarSearchButtonClicked:를 구현하고 거기에 당신의 검색 코드를 삽입해야 들어

@implementation SearchViewController 

@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive; 

- (void)viewDidLoad { 
    // restore search settings if they were saved in didReceiveMemoryWarning. 
    if (self.savedSearchTerm) { 
     [self.searchDisplayController setActive:self.searchWasActive]; 
     [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex]; 
     [self.searchDisplayController.searchBar setText:savedSearchTerm]; 
     self.savedSearchTerm = nil; 
    } 
} 

- (void)viewDidUnload { 
    // Save the state of the search UI so that it can be restored if the view is re-created. 
    self.searchWasActive = [self.searchDisplayController isActive]; 
    self.savedSearchTerm = [self.searchDisplayController.searchBar text]; 
    self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex]; 
    self.filteredListContent = nil; 
} 

- (void)dealloc { 
    [listContent release]; 
    [filteredListContent release]; 
    [super dealloc]; 
} 

- (void)setData { 
    self.listContent = [NSMutableArray arrayWithCapacity:3]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"AAA" name:@"Test1"]]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test2"]]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test3"]]; 

    // create a filtered list 
    self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]]; 
    [self.tableView reloadData]; 
    self.tableView.scrollEnabled = YES; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list. 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [self.filteredListContent count]; 
    } else { 
     return [self.listContent count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *kCellID = @"cellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    /* If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list. */ 
    SearchObjects *searchObject = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     searchObject = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } else { 
     searchObject = [self.listContent objectAtIndex:indexPath.row]; 
    } 
    cell.textLabel.text = searchObject.name; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // HERE IS THE SOURCE CODE FOR PUSHING TO THE NEXT VIEW 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    // DO SOME CALCULATIONS… AND THE setData METHOD IS CALLED 
} 

- (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 whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. */ 
    for (SearchObjects *searchObject in listContent) { 
     if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) { 
     NSComparisonResult result = [searchObject.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
      if (result == NSOrderedSame) { 
       [self.filteredListContent addObject:searchObject]; 
      } 
     } 
    } 
} 

- (void)filterContentForScope:(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 whose type matches the scope (if selected); add items that match to the filtered array. */ 
    for (SearchObjects *searchObject in listContent) { 
     if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) { 
      [self.filteredListContent addObject:searchObject]; 
     } 
    } 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];  
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { 
    [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 
@end 

답변

0

문제가 해결되었습니다. 의견을 참조하십시오.

0

. – searchBarTextDidEndEditing: 또는 – searchBar:textDidChange:과 같은 다른 정보도 구현해야하며 검색을 수행하지 않아야합니다.

두 번째 질문에 대해서는 이미 검색 한 후에 발생하는 것을 확인하기 위해 – searchBarSearchButtonClicked:의 대리인을 다시 사용하여 tableView를 다시로드하기 만하면됩니다. 이를 수행하려면 [tableView reloadData]을 사용할 수 있습니다.

+0

음, 이해가 안됩니다. 이미 "searchBarSearchButtonClicked :"메소드가 있습니다. 이 요청을 서버에 보내고 "setData"메서드는 TableView 데이터를 설정하고 reloadData를 수행하지만 작동하지 않습니다. – Tim

+0

데이터를 다시로드하지 못하도록 searchBarTextDidChange를 재정의 했습니까? – mjdth

+0

나는이 문제를 해결했다. 플래그를 설정하고 플래그가 true 인 경우에만 검색이 완료되었음을 나타냅니다. 그렇다면 YES를 반환합니다. 대리자 메서드 searchDisplayController : controller shouldReloadTableFor ... 다른 문제는 listContent에있었습니다. reloadData를 수행하지만 filteredContentList에 포커스가 있습니다! – Tim