2010-07-15 5 views
0

저는이 응용 프로그램에 익숙하지 않고 UISearchBar을 사용합니다. 검색 할 단어를 입력하면 응용 프로그램이 충돌합니다.iPhone : 검색 막대가 TableView에서 사용되면 응용 프로그램이 충돌 함

내 프로젝트에서 나는 sqlite를 사용하고 있으며 값은 AppDelegate의 db에서 가져오고 이라는 이름의 docArray에 저장됩니다. 오류 다음

RootViewController.m

- (void)viewDidLoad{ 
     listOfItems = [[NSMutableArray alloc] init]; 
     NSDictionary *value = [NSDictionary dictionaryWithObject:appDelegate.docArray forKey:@"doctors"]; 
     [listOfItems addObject:value]; 
     // Intialize copy array that will store the result of search result 
     copyListOfItems = [[NSMutableArray alloc] init]; 
      // Add Search bar to main view ..... 
     self.tableView.tableHeaderView = searchBar; 
     searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
     searching = NO; 
     letUserSelectRow = YES; 
    } 

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


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    { 
     if (searching) 
     return [copyListOfItems count]; 
    else 
     return [appDelegate.arryData 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] autorelease]; 
    } 

    if (searching) 
    { 
     waitsup *wu = [copyListOfItems objectAtIndex:indexPath.row]; 

     [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]]; 
     cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"]; 
     [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
     else 
    { 
     waitsup *wu = [appDelegate.arryData objectAtIndex:indexPath.row]; 

     [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]]; 
     cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"]; 
     [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 

     return cell; 
    } 

    -(void)searchBarTextDidBeginEditing :(UISearchBar *)theSearchBar { 
    NSLog(@"Now in searchBarTextDidBeginEditing"); 

    searching = YES; 
    letUserSelectRow = NO; 

    self.tableView.scrollEnabled = NO; 

    // Add Done button ........ 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
               initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
               target:self action:@selector(doneSearching_clicked:)] 
               autorelease]; 

} 


-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath :(NSIndexPath*)indexPath { 
    NSLog(@"will select Row At Index Path"); 

    if (letUserSelectRow) 
    { 
     return indexPath; 
    } 
    else 
    { 
     return nil; 
    } 

} 


-(void)searchBar :(UISearchBar*)theSearchBar textDidChange :(NSString*)searchText { 
    NSLog(@"textDidChange"); 

    // it is done so that data can be selected as new search 
    [copyListOfItems removeAllObjects]; 

    if ([searchText length] > 0) 
    { 
     searching = YES; 
     letUserSelectRow = YES; 
     self.tableView.scrollEnabled = YES; 

     [self searchTableView]; 
    } 
    else 
    { 
     searching = NO; 
     letUserSelectRow = NO; 
     self.tableView.scrollEnabled = NO; 
    } 

    [self.tableView reloadData]; 

} 


-(void)searchBarSearchButtonClicked :(UISearchBar*)theSearchBar { 
    NSLog(@"searchBarSearchButtonClicked"); 
    [self searchTableView]; 
} 


-(void)searchTableView { 
    NSLog(@"searchTableView"); 

    NSString *searchText = searchBar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dictionary in listOfItems) 
    { 
     NSArray *array = [dictionary objectForKey:@"first_name"]; 
     [searchArray addObjectsFromArray:array]; 
    } 


    for (waitsup *sTemp in searchArray) 
    { 
     NSRange titleResultsRange = [[NSString stringWithFormat:@"%@ %@",sTemp.first_name, sTemp.last_name] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
     { 
      NSLog(@"%@", sTemp); 
      [copyListOfItems addObject:sTemp]; 
     } 

    } 


    [searchArray release]; 
    searchArray = nil; 
} 


-(void)doneSearching_clicked:(id)sender { 
    NSLog(@"doneSearching_clicked"); 

    searchBar.text = @" "; 
    [searchBar resignFirstResponder]; 

    letUserSelectRow = YES; 
    searching = NO; 

    self.navigationItem.rightBarButtonItem = nil; 
    self.tableView.scrollEnabled = YES; 

    [self.tableView reloadData];  
} 

콘솔 쇼 : 한 가지, 코드를 응축 해주십시오

2010-07-22 13:36:16.002 wu2[1077:207] Processing Element: array 

2010-07-22 13:36:16.004 wu2[1077:207] Processing Element: XML_Serializer_Tag 

2010-07-22 13:36:16.005 wu2[1077:207] Processing Element: id 

2010-07-22 13:36:16.005 wu2[1077:207] Processing Value: 1 

2010-07-22 13:36:16.006 wu2[1077:207] Processing Element: first_name 

2010-07-22 13:36:16.007 wu2[1077:207] Processing Value: Prateek 

2010-07-22 13:36:16.007 wu2[1077:207] Processing Element: middle_name 

2010-07-22 13:36:16.008 wu2[1077:207] Processing Value: K 

2010-07-22 13:36:16.008 wu2[1077:207] Processing Element: last_name 

2010-07-22 13:36:16.009 wu2[1077:207] Processing Value: Bhanot 

2010-07-22 13:36:16.009 wu2[1077:207] Processing Element: title 

2010-07-22 13:36:16.010 wu2[1077:207] Processing Value: Er. 

2010-07-22 13:36:16.011 wu2[1077:207] Processing Element: org_name 

2010-07-22 13:36:16.011 wu2[1077:207] Processing Value: Cyber 

2010-07-22 13:36:16.012 wu2[1077:207] Processing Element: upin 

2010-07-22 13:36:16.014 wu2[1077:207] Processing Value: 34242 

2010-07-22 13:36:16.014 wu2[1077:207] Processing Element: npi 

2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: 2343242 

2010-07-22 13:36:16.015 wu2[1077:207] Processing Element: address1 

2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: Maler Kotla 

2010-07-22 13:36:16.016 wu2[1077:207] Processing Element: address2 

2010-07-22 13:36:16.016 wu2[1077:207] Processing Value: Mohali 

2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: city 

2010-07-22 13:36:16.018 wu2[1077:207] Processing Value: Chandigarh 

2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: state 

2010-07-22 13:36:16.019 wu2[1077:207] Processing Value: Punja 

2010-07-22 13:36:16.020 wu2[1077:207] Processing Element: zipcode 

2010-07-22 13:36:16.020 wu2[1077:207] Processing Value: 12345 

2010-07-22 13:36:16.021 wu2[1077:207] Processing Element: country 

2010-07-22 13:36:16.022 wu2[1077:207] Processing Value: India 

2010-07-22 13:36:16.022 wu2[1077:207] Processing Element: county 

2010-07-22 13:36:16.023 wu2[1077:207] Processing Element: office_phone1 

2010-07-22 13:36:16.025 wu2[1077:207] Processing Value: 13123131 

2010-07-22 13:36:16.027 wu2[1077:207] Processing Element: fax1 

2010-07-22 13:36:16.028 wu2[1077:207] Processing Value: 2131231 

2010-07-22 13:36:16.028 wu2[1077:207] Processing Element: gender 

2010-07-22 13:36:16.029 wu2[1077:207] Processing Value: male 

2010-07-22 13:36:16.030 wu2[1077:207] Processing Element: status 

2010-07-22 13:36:16.030 wu2[1077:207] Processing Value: active 

2010-07-22 13:36:16.031 wu2[1077:207] Processing Element: profiletype 

2010-07-22 13:36:16.031 wu2[1077:207] Processing Value: user 

2010-07-22 13:36:16.032 wu2[1077:207] Processing Element: entity 

2010-07-22 13:36:16.033 wu2[1077:207] Processing Value: practice 

2010-07-22 13:36:16.034 wu2[1077:207] Processing Element: XML_Serializer_Tag 

2010-07-22 13:36:16.035 wu2[1077:207] Processing Element: id 

2010-07-22 13:36:16.035 wu2[1077:207] Processing Value: 2 

2010-07-22 13:36:16.036 wu2[1077:207] Processing Element: first_name 

2010-07-22 13:36:16.036 wu2[1077:207] Processing Value: Hitu 

2010-07-22 13:36:16.037 wu2[1077:207] Processing Element: middle_name 

2010-07-22 13:36:16.038 wu2[1077:207] Processing Value: k 

2010-07-22 13:36:16.038 wu2[1077:207] Processing Element: last_name 

2010-07-22 13:36:16.039 wu2[1077:207] Processing Value: bansal 

2010-07-22 13:36:16.040 wu2[1077:207] Processing Element: title 

2010-07-22 13:36:16.040 wu2[1077:207] Processing Value: Doctor 

2010-07-22 13:36:16.041 wu2[1077:207] Processing Element: org_name 

2010-07-22 13:36:16.041 wu2[1077:207] Processing Value: Cyebertron 

2010-07-22 13:36:16.042 wu2[1077:207] Processing Element: upin 

2010-07-22 13:36:16.042 wu2[1077:207] Processing Value: 34242 

2010-07-22 13:36:16.043 wu2[1077:207] Processing Element: npi 

2010-07-22 13:36:16.043 wu2[1077:207] Processing Value: 2343242 

2010-07-22 13:36:16.044 wu2[1077:207] Processing Element: address1 

2010-07-22 13:36:16.045 wu2[1077:207] Processing Value: Mohali 

2010-07-22 13:36:16.045 wu2[1077:207] Processing Element: address2 

2010-07-22 13:36:16.046 wu2[1077:207] Processing Value: chd 

2010-07-22 13:36:16.046 wu2[1077:207] Processing Element: city 

2010-07-22 13:36:16.047 wu2[1077:207] Processing Value: Chandigarh 

2010-07-22 13:36:16.048 wu2[1077:207] Processing Element: state 

2010-07-22 13:36:16.048 wu2[1077:207] Processing Value: Punja 

2010-07-22 13:36:16.049 wu2[1077:207] Processing Element: zipcode 

2010-07-22 13:36:16.049 wu2[1077:207] Processing Value: 12345 

2010-07-22 13:36:16.050 wu2[1077:207] Processing Element: country 

2010-07-22 13:36:16.050 wu2[1077:207] Processing Value: India 

2010-07-22 13:36:16.051 wu2[1077:207] Processing Element: county 

2010-07-22 13:36:16.051 wu2[1077:207] Processing Value: jljljlkj 

2010-07-22 13:36:16.052 wu2[1077:207] Processing Element: office_phone1 

2010-07-22 13:36:16.053 wu2[1077:207] Processing Value: 12-32-3-3 

2010-07-22 13:36:16.053 wu2[1077:207] Processing Element: fax1 

2010-07-22 13:36:16.054 wu2[1077:207] Processing Value: 12331331 

2010-07-22 13:36:16.054 wu2[1077:207] Processing Element: gender 

2010-07-22 13:36:16.055 wu2[1077:207] Processing Value: male 

2010-07-22 13:36:16.056 wu2[1077:207] Processing Element: status 

2010-07-22 13:36:16.056 wu2[1077:207] Processing Value: active 

2010-07-22 13:36:16.057 wu2[1077:207] Processing Element: profiletype 

2010-07-22 13:36:16.057 wu2[1077:207] Processing Value: dr411 

2010-07-22 13:36:16.058 wu2[1077:207] Processing Element: entity 

2010-07-22 13:36:16.058 wu2[1077:207] Processing Value: provider 

2010-07-22 13:36:16.059 wu2[1077:207] No Errors 

2010-07-22 13:36:20.923 wu2[1077:207] Now in searchBarTextDidBeginEditing 

2010-07-22 13:36:22.899 wu2[1077:207] textDidChange 

2010-07-22 13:36:22.900 wu2[1077:207] searchTableView 

2010-07-22 13:36:22.901 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:22.985 wu2[1077:207] textDidChange 

2010-07-22 13:36:22.986 wu2[1077:207] searchTableView 

2010-07-22 13:36:22.987 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:23.073 wu2[1077:207] textDidChange 

2010-07-22 13:36:23.074 wu2[1077:207] searchTableView 

2010-07-22 13:36:23.075 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:23.249 wu2[1077:207] textDidChange 

2010-07-22 13:36:23.250 wu2[1077:207] searchTableView 

2010-07-22 13:36:23.250 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:23.449 wu2[1077:207] textDidChange 

2010-07-22 13:36:23.450 wu2[1077:207] searchTableView 

2010-07-22 13:36:23.450 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:23.625 wu2[1077:207] textDidChange 

2010-07-22 13:36:23.626 wu2[1077:207] searchTableView 

2010-07-22 13:36:23.626 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:23.777 wu2[1077:207] textDidChange 

2010-07-22 13:36:23.778 wu2[1077:207] searchTableView 

2010-07-22 13:36:23.778 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:24.377 wu2[1077:207] searchBarSearchButtonClicked 

2010-07-22 13:36:24.377 wu2[1077:207] searchTableView 

2010-07-22 13:36:24.378 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:26.157 wu2[1077:207] doneSearching_clicked 

2010-07-22 13:36:26.161 wu2[1077:207] textDidChange 

2010-07-22 13:36:26.161 wu2[1077:207] searchTableView 

2010-07-22 13:36:26.162 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:26.162 wu2[1077:207] <waitsup: 0x6e16100> 

2010-07-22 13:36:27.071 wu2[1077:207] Now in searchBarTextDidBeginEditing 

2010-07-22 13:36:28.329 wu2[1077:207] textDidChange 

2010-07-22 13:36:28.330 wu2[1077:207] searchTableView 

2010-07-22 13:36:30.221 wu2[1077:207] doneSearching_clicked 

2010-07-22 13:36:30.224 wu2[1077:207] textDidChange 

2010-07-22 13:36:30.224 wu2[1077:207] searchTableView 

2010-07-22 13:36:30.225 wu2[1077:207] <waitsup: 0x6e14fc0> 

2010-07-22 13:36:30.226 wu2[1077:207] <waitsup: 0x6e16100> 
+0

업데이트 된 정보를 제공해 주셔서 감사합니다. 아래에서 제안 된 해결 방법을 참조하십시오. 이것으로부터 배울 점 중 하나는 핵심 오류가 아래에 설명 된 이유 때문에'- [waitsup rangeOfString : options :] : 인스턴스 0x6d0f8e0으로 보낸 인식 할 수없는 선택자 '입니다. – mvds

답변

0

그래서 우리는 모든 줄 바꿈을 스크롤 할 필요가 없습니다. 또한,이 테이블보기의는 dataSource이며, 만약 그렇다면, 어디에 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

가 실패하고 충돌의 원인이 될 수 있습니다 copyListOfItems에 액세스해야합니다.

콘솔이 충돌 할 때 표시되는 내용은 무엇입니까?

편집 : 추가 정보를 제공해 주셔서 감사합니다. 귀하의 문제는 여기에있다 :

for (NSString *sTemp in searchArray) 

searchArray는 "waitsup"객체, 내가 NSString하지 생각이 포함되어 있습니다. 이 포인터를 NSString 포인터로 캐스팅했지만 실제로는 NSString 개체를 가리키는 것은 사용자의 책임입니다. 따라서 waitsup에서 rangeOfString이 호출되는 오류가 발생합니다.이 오류는 "인식 할 수 없음"입니다. rangeOfString 메서드를 직접 제공하지 않았다면 waitsup 개체에서 rangeOfString을 호출 할 수 없습니다. 코드의 나머지 부분을 보면

, 나는이 같은 문제를 해결할 수 있다고 생각 :

for (waitsup *sTemp in searchArray) { 
    NSRange titleResultsRange = [sTemp.name_last rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
    if (titleResultsRange.length > 0) 
     [copyListOfItems addObject:sTemp]; 
} 

하지만 만 name_last가 검색됩니다. 이 필드를 확장하여 더 많은 필드를 포함시키는 것은 쉽습니다.

+0

고마워요.하지만이 앱이 고장난 후에도 하나의 알파벳 만 입력 할 수있는 문제가 있습니다. –

+0

지금 가지고있는 코드와 지금 얻은 오류로 질문을 업데이트 할 수 있습니까? – mvds

+0

이제 확인할 수 있습니다. –

관련 문제